addeventlistener and removeeventlistener in JavaScript

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss assigning event handlers in JavaScript using the following special methods
addEventListener
removeEventListener
attachEvent
detachEvent

Internet Explorer 9 (and later versions) & all the other modern browsers support addEventListener() and removeEventListener() methods.

Sytnax for assigning event handler using addEventListener() method

Sytnax for removing event handler using removeEventListener() method

Please note : The third parameter phase is usually set to false as it is not used.

Example : In this example, we are passing values for all the 3 parameters including phase.

[input type="button" value="Click me" id="btn"/]
[script type="text/javascript"]

function changeColorOnMouseOver()
{
}

function changeColorOnMouseOut()
{
}
[/script]

Since the third parameter "phase" is optional you can omit it if you wish.

Example : This example demonstrates removing event handlers.

[input type="button" value="Click me" id="btn"/]
[input type="button" value="Remove Event Handlers" onclick="removeEventHandlers()" /]
[script type="text/javascript"]

function changeColorOnMouseOver()
{
}

function changeColorOnMouseOut()
{
}

function removeEventHandlers()
{
}
[/script]

Using this approach you can assign more than one event handler method to a given event. The order in which handler methods are executed is not defined. In this example, 2 event handler methods (clickHandler1 & clickHandler3) are assigned to click event of the button control. When you click the button both the handler methods are executed.

[input type="button" value="Click me" id="btn"/]
[script type="text/javascript"]

function clickHandler1()
{
alert("Handler 1");
}

function clickHandler2()
{
alert("Handler 2");
}
[/script]

attachEvent() and detachEvent() methods only work in Internet Explorer 8 and earlier versions.

Sytnax for assigning event handler using attachEvent() method

Sytnax for removing event handler using detachEvent() method

Example : This example will only work in Internet Explorer 8 and earlier versions.

[input type="button" value="Click me" id="btn"/]
[script type="text/javascript"]

function clickEventHandler()
{
alert("Click Handler");
}
[/script]

Cross browser solution : For the above example to work in all browsers, modify the script as shown below.

[input type="button" value="Click me" id="btn"/]
[script type="text/javascript"]

{
}
else
{
}

function clickEventHandler()
{
alert("Click Handler");
}
[/script]
Рекомендации по теме
Комментарии
Автор

6:28
*The order in which handler methods are executed is not defined.*
May I ask, why so?

sayantaniguha
Автор

Excellent video, but 'btn' variable I never initializes, how does browser know it refers to the control with id = 'btn' ?

liang-hsuanma
Автор

What happends if I decide to add content one time when button is clicked?
How could I just add it once?

gabrielcamilo
Автор

Great Work.!! Could you please share some videos on React JS from beginners to intermediate level??

steffikashyap
Автор

Hi venkat,
If there are two html elements nested and one element event handler is bubbling and other is capture, then how does event propagation wok. Thanks in advance

vinayreddy
Автор

why we don't use parentheses for calling functions for DOM objects?

dgloria
Автор

you said btn.addEvenetLisner
how visual studio didn't show error !
i think you should get element by id in variable btn then using it didn't it

mahmoud
Автор

Great work
need help on design patterns. ...

dgcrush
Автор

IE is ok but AOL explorer is the best browser IMO

ralphedgar