9

Here is the code:

$("div").on("click",function(){
       console.log("click");
});
$("div").on("click.plugin", function(){
       console.log("click.plugin");
});
$("button").click(function() {
      $("div").trigger("click!");    
});

and the HTML:

<div>test.</div>
<button >Trigger event according to namespace</button>

When I run the code under jQuery 1.8.3, it works. When I click button, it logs click in the console.

But when I change to jQuery 1.9.1, nothing happens when I press the button. It seems like the exclamation mark doesn't work anymore in 1.9.1.

I can't find this change in the 1.9 upgrade guide. Does anybody know why?

8
  • 5
    Never seen that before. What is it supposed to do? Commented May 9, 2013 at 10:00
  • @juhana never seen that either. Docs say it should trigger only not-namespaced handlers. Commented May 9, 2013 at 10:04
  • 1
    @bažmegakapa Where did you read in Docs? Can you point that out to me? Commented May 9, 2013 at 10:08
  • I can find only non-official mentions. Undocumented feature? Commented May 9, 2013 at 10:13
  • 1
    docs.jquery.com/Events/trigger Commented May 9, 2013 at 10:14

2 Answers 2

5

Use .$ instead of !

$("button").click(function() {
      $("div").trigger("click.$");    
});

Demo [Credits: Tim B James]

Sign up to request clarification or add additional context in comments.

6 Comments

@bažmegakapa, The funny thing about that page was that it was linked to another SO question here. Ha ha. But seen the demo and it works.
@TimBJames, Thanks I will add that to the answer.
@bažmegakapa, Seems like a undocumented hack. Cannot find it anywhere and am too lazy to dig the source :(
jQuery is not using regex to find the event name. This only happens to work by accident.
The reason why appending '.$' works is because of the if ( type.indexOf(".") >= 0 ) part (see my answer). Since there is no event namespace '$' it seems as if only click was triggered.
|
4

This is how jQuery 1.8.3 looks like:

trigger: function( event, data, elem, onlyHandlers ) {

    // Don't do events on text and comment nodes
    if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
        return;
    }

    // Event object or event type
    var cache, exclusive, i, cur, old, ontype, special, handle, eventPath, bubbleType,
        type = event.type || event,
        namespaces = [];

    // focus/blur morphs to focusin/out; ensure we're not firing them right now
    if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
        return;
    }

    if ( type.indexOf( "!" ) >= 0 ) {
        // Exclusive events trigger only for the exact event (no namespaces)
        type = type.slice(0, -1);
        exclusive = true;
    }

    if ( type.indexOf( "." ) >= 0 ) {
        // Namespaced trigger; create a regexp to match event type in handle()
        namespaces = type.split(".");
        type = namespaces.shift();
        namespaces.sort();
    }

    // ...

Notice the "Exclusive events trigger only for the exact event" section.

And this is jQuery 1.9.1:

trigger: function( event, data, elem, onlyHandlers ) {
    var handle, ontype, cur,
        bubbleType, special, tmp, i,
        eventPath = [ elem || document ],
        type = core_hasOwn.call( event, "type" ) ? event.type : event,
        namespaces = core_hasOwn.call( event, "namespace" ) ? event.namespace.split(".") : [];

    cur = tmp = elem = elem || document;

    // Don't do events on text and comment nodes
    if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
        return;
    }

    // focus/blur morphs to focusin/out; ensure we're not firing them right now
    if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
        return;
    }

    if ( type.indexOf(".") >= 0 ) {
        // Namespaced trigger; create a regexp to match event type in handle()
        namespaces = type.split(".");
        type = namespaces.shift();
        namespaces.sort();
    }

    // ...

Here the entire section is missing (it's also not in the omitted bit).

It seems as if jQuery has dropped support for this feature. The variable exclusive has been removed from the whole source.

Looking at the source of version 1.9.1 I don't see a way for you to get the desired functionality without resorting to hacks.

6 Comments

Thanks for all you guys's answers, and do we have jQuery engineers in this website, maybe we can tell them to put this change in jQuery 1.9.1 upgrade guide.
jQuery has a bug tracking system. Open a ticket and see what happens.
Thanks, Tomalak, I've already post the issue on tracking system and got the resonse, they will add it in upgrade guide. Here is the link: bugs.jquery.com/ticket/13873
On this line jQuery seems to be using regex to match the event names isn't it?
It seems like it, but there is no documentaion for it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.