Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between true and false in javascript eventlistener

Tags:

javascript

i have a doubt in eventlistener concept.What is the difference between bellow two codes i have doubt in the true/false section.no change happens when i replace the 1st code with the second code in my practice code.

a.addEventListener("click", modifyText, true);
a.addEventListener("click", modifyText, false);
like image 812
DjangoDev Avatar asked Feb 11 '13 07:02

DjangoDev


People also ask

What is true and false in addEventListener?

true and false in addEventListener is Boolean that specifies whether the event needs to be captured or not. Here's the syntax and detail: object. addEventListener (eventName, function, useCapture); eventName: String that specifies the name of the event to listen for.

What is Eventlistener in JavaScript?

An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.

What does false in event listener mean?

addEventListener("click", first, true); when clicking child element, first method will be called before second . By default, the useCapture flag is set to false which means you handler will only be called during event bubbling phase.

What is onclick return false?

onclick = function (e) { return false; }; According to the HTML 5 specifications, return false will cancel the event except the mouseover event.


1 Answers

true and false in addEventListener is Boolean that specifies whether the event needs to be captured or not.

Here's the syntax and detail:

object.addEventListener (eventName, function, useCapture);

eventName: String that specifies the name of the event to listen for. This parameter is case sensitive!

function: Represents the event listener function to be called when the event occurs. When an event occurs, an event object is initialized and passed to the event handler as the first parameter. The type of the event object depends on the current event.

useCapture: Boolean that specifies whether the event needs to be captured or not. One of the following values:

false -> Register the event handler for the bubbling phase. 
true -> Register the event handler for the capturing phase.

Bubbling and Capturing Phases:

bubbling: the event object propagates through the target's ancestors in reverse order, starting with the target's parent and ending with the defaultView. This phase is also known as the bubbling phase. Event listeners registered for this phase must handle the event after it has reached its target.

capturing: the event object must propagate through the target's ancestors from the defaultView to the target's parent. This phase is also known as the capturing phase. Event listeners registered for this phase must handle the event before it reaches its target.

For more detail on event flow: DOM Event Architecture

like image 150
AlphaMale Avatar answered Oct 24 '22 05:10

AlphaMale