Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is event.namespace undefined for click event?

Tags:

jquery

Given this:

HTML

<button id="btn">Click me</button>
<br/>
<div id="result"></div>

JS

$("#btn").on("click.mynamespace", function (e) {
    $("#result").text("namespace: " + e.namespace);
});

Why is namespace always undefined?

EDit:

What I want to be able to do is connect multiple instances of an event handler to a button's click event. Then, when the button is clicked, each instance is called, in turn. Each instance of the event handler then needs to disconnect itself, so that if the button is clicked again, that handler instance is not called.

like image 436
slippyr4 Avatar asked Mar 26 '13 19:03

slippyr4


1 Answers

The namespace property is only defined if the event was triggered using jQuery's .trigger or .triggerHandler methods. Otherwise, how would jQuery know which namespace to use if there were multiple?

http://jsfiddle.net/GYyef/2/

http://api.jquery.com/event.namespace/

If your goal is to have the event only happen once, the .one() method would be more appropriate.

like image 116
Kevin B Avatar answered Sep 23 '22 20:09

Kevin B