Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger event in jquery without a namespace

With the current version of jQuery (1.8.3) you can still use the following code (showing both a click with and without a namespace):

$("span")
    .on("click", function(e) { console.log('click without a namespace'); })
    .on("click.namespace", function(e) { console.log('click with a namespace'); })
    .trigger("click!"); // "click without a namespace"

JSFiddle

However, that seems to be removed in 1.9. Is there a way in the new version of jQuery (the test version) to replicate this feature? Or do I have to create a custom event like this:

$("span")
    .on("click", function(e) { $(this).trigger("customevent"); })
    .on("click.namespace", function(e) { console.log('click with a namespace'); })
    .on("customevent", function(e) { console.log('"click" without a namespace'); })
    .trigger("customevent");

JSFiddle

like image 970
voigtan Avatar asked Dec 03 '12 20:12

voigtan


People also ask

What does jQuery use the event namespace for?

The event. namespace property in jQuery is used to return the custom namespace whenever the event is triggered. It is used to handle tasks differently depending on the namespace used.

What is an event namespace?

The event. namespace property returns the custom namespace when the event was triggered. This property can be used by plugin authors to handle tasks differently depending on the namespace used.

How do you call one event from another event in jQuery?

$('#b1'). on('click', function(){ $('#select_item'). trigger('change'); }); $('#select_item'). on('change', function(){ var jbyr = $(this).

What does .on do in Javascript?

The on() is an inbuilt method in jQuery which is used to attach one or more event handlers for the selected elements and child elements in the DOM tree.


1 Answers

You can call that using

.trigger("click.$")

as we found it is because regex

http://jsfiddle.net/oceog/RczDZ/10/

like image 183
zb' Avatar answered Oct 24 '22 18:10

zb'