How to remove the event listener. [Resolved]

Posted by Programmer123 under JavaScript on 9/24/2013 | Points: 10 | Views : 1126 | Status : [Member] | Replies : 4
I have add a event listener and now I want to remove this. Can any one tell me how to remove the event listener.




Responses

Posted by: Murugavelmsc on: 9/25/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.removeEventListener
http://www.javascripter.net/faq/removeeventlistenerdetachevent.htm
http://help.dottoro.com/ljahxbsx.php

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/25/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Sample code:
1) This example illustrates the use of the removeEventListener and detachEvent methods:
<head>
<script type="text/javascript">
function OnRedClick () {
alert ("A click event has occurred on the red button.");
}

function AddEventHandler () {
var redButton = document.getElementById ("redButton");
if (redButton.addEventListener) { // all browsers except IE before version 9
redButton.addEventListener ("click", OnRedClick, false);
}
else {
if (redButton.attachEvent) { // IE before version 9
redButton.attachEvent ('onclick', OnRedClick);
}
}
}

function RemoveEventHandler () {
var redButton = document.getElementById ("redButton");
if (redButton.removeEventListener) { // all browsers except IE before version 9
redButton.removeEventListener ("click", OnRedClick, false);
}
else {
if (redButton.detachEvent) { // IE before version 9
redButton.detachEvent ('onclick', OnRedClick);
}
}
}
</script>
</head>
<body>
Click on the red button when the 'click' event has a listener and when it does not.<br />
<button onclick="AddEventHandler ();">Add a 'click' event listener to the red button</button>
<button onclick="RemoveEventHandler ();">Remove the event listener</button>
<br /><br />
<button id="redButton" style="background-color:red">Red button</button>
</body>


2)
canvas.addEventListener('click', function(event) {
click++;
if(click == 50) {
this.removeEventListener('click',arguments.callee,false);
}



References:
http://www.javascripter.net/faq/removeeventlistenerdetachevent.htm
http://help.dottoro.com/ljahxbsx.php

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/25/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can see the below code:-

<script type="text/javascript">
document.getElementById("hitme4").removeEventListener("click", hitme4, false);
</script>


Key Events

"onkeydown", "onkeypress", "onkeyup" events are supported both in ie and standards-based browsers.

<script type="text/javascript">
function setStatus(name,evt) {
evt = (evt) ? evt : ((event) ? event : null); /* ie or standard? */
var charCode = evt.charCode;
var status = document.getElementById("keyteststatus");
var text = name +": "+evt.keyCode;
status.innerHTML = text;
status.textContent = text;
}
</script>
<form action="">
<input type="text" name="keytest" size="1" value=""
onkeyup="setStatus('keyup',event)"
onkeydown="setStatus('keydown',event)"
/>
<p id="keyteststatus">status</p>
</form>


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 9/25/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
try this link it might be helpful to you
http://stackoverflow.com/questions/15100576/how-to-remove-an-event-listener-in-javascript

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response