buch
buch

Reputation: 1

How to remove an eventlistener from a class?

I have a problem removing an event listener. I get a changeGirl.off("click") is not a function error when I run this script. I need to remove the event listener, everything else is working perfectly. Any ideas?

$( document ).ready(function() {
	$(".changeGirl").each(function() {
		var changeGirl = this;
		
		changeGirl.addEventListener("click",function() {
			if (window.XMLHttpRequest) {
				xmlhttp=new XMLHttpRequest();
			} else {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4 && xmlhttp.status==200) {
					changeGirl.innerHTML = xmlhttp.responseText;
					changeGirl.off("click");
				}
			}
			xmlhttp.open("GET","getUsers.php?girls="+$(this).attr("data-position"),true);
			xmlhttp.send();
		});
	});
});

Upvotes: 0

Views: 61

Answers (1)

Raphael Deiana
Raphael Deiana

Reputation: 750

You can try to extract the function from your addEventListener and name it for example yourFunction, then use removeEventListener:

changeGirl.addEventListener("click", yourFunction);
changeGirl.removeEventListener("click", yourFunction, true);

Upvotes: 1

Related Questions