Skip to content Skip to sidebar Skip to footer

False In Addeventlistener?

in this below line: third parameter is kept false. what is this attribute?? var el = document.getElementById('outside'); el.addEventListener('click', modifyText, false);

Solution 1:

It controls if the event will bubble up or down the DOM tree.

Click on three in the example below (and linked here). Then change false to true and repeat.

If useCapture is set to false, it will bubble up the tree and you will get x3, x2, x1, if it is set to true, it will bubble down and you will get x1, x2, x3.

See:

<!DOCTYPE html><html><head><metacharset=utf-8 /><title>JS Bin</title></head><body><divid="x1">One
    <divid="x2">Two
      <divid="x3">Three
      </div></div></div><script>functionme() { alert(this.id); };
    var divs = document.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
      divs[i].addEventListener('click', me, false);
    }
  </script></body></html>

Solution 2:

That's the useCapture parameter. From documentation

useCaptureOptional

If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events for a detailed explanation. If not specified, useCapture defaults to false.

Read more about this topic here.

Solution 3:

According to this documentation, it's wether or not to use "capture"

If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events for a detailed explanation. If not specified, useCapture defaults to false. Note: useCapture became optional only in more recent versions of the major browsers; for example, it was not optional prior to Firefox 6. You should provide this parameter for broadest compatibility.

Post a Comment for "False In Addeventlistener?"