Results for evenements : 5

1 - trigger event on HTML element using JavaScript - 30 seconds of code
const triggerEvent = (el, eventType, detail) =>
  el.dispatchEvent(new CustomEvent(eventType, { detail }));

const myElement = document.getElementById('my-element');
myElement.addEventListener('click', e => console.log(e.detail));

triggerEvent(myElement, 'click');
// The event listener will log: null

triggerEvent(myElement, 'click', { username: 'bob' });
// The event listener will log: { username: 'bob' }
			
2 - listen for a click outside of an element in JavaScript? - 30 seconds of code
const onClickOutside = (element, callback) => {
  document.addEventListener('click', e => {
    if (!element.contains(e.target)) callback();
  });
};

onClickOutside('#my-element', () => console.log('Hello'));
// Will log 'Hello' whenever the user clicks outside of #my-element
			
4 - fonction trigger pour déclencher un évènement
       if (typeof obj=='string'){obj=document.getElementById(obj);}
            if (window.CustomEvent) {
              var event = new CustomEvent(ev, {detail: {some: 'data'}})
            } else {
              var event = document.createEvent('CustomEvent')
              event.initCustomEvent('ev', true, true, {some: 'data'})
            }

            obj.dispatchEvent(event)
        }
			
5 - Raccourci clavier textarea pour valider [Résolu]
<script type="text/javascript">
window.onload = function() {

    /* <textarea id="comment_textarea" ...></textarea> */
    var textarea = document.getElementById('comment_textarea');
    
    textarea.onkeydown = function(e) {
        e = e || event; // cross-browser (quand IE en fait � sa t�te)
        
        /* Envoyer le formulaire avec Ctrl+Entrer */
        if(e.keyCode == 13 && e.ctrlKey) {
            this.form.submit();
        }
        
        /* Envoyer le formulaire avec Entrer mais autoriser
         * les retours � la ligne avec Ctrl+Entrer */
        if(e.keyCode == 13 && !e.ctrlKey) {
            this.form.submit();
            return false;
        } else if (e.keyCode == 13) {
            this.value += "\n";
        }
    };
};
</script>