8

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

11
  • Can you be more specific on what is not working? Commented Dec 3, 2012 at 20:48
  • @Vega from jquery 1.9 I will not be able to use click! to only trigger event bounded on click only (look at the test links you will see that only one "test" will be logged), the second code I posted will "fix" that, but that means that I have to bind one more event. Commented Dec 3, 2012 at 20:50
  • Your two samples aren't exactly the same... if you make them both the same, they both perform the same. What are you trying to say/do? at this point it seems like you're asking for it to do what it is doing. Commented Dec 3, 2012 at 21:02
  • the second example is how I could do it with 1.9 comes out (or just using jQuery edge on jsfiddle), my main question is is there another way to keep more or less the old code and still keep this undocumented feature. Commented Dec 3, 2012 at 21:04
  • jsfiddle.net/oceog/RczDZ/5 check this fiddle and tell what is expected result after one click Commented Dec 3, 2012 at 21:05

1 Answer 1

12

You can call that using

.trigger("click.$")

as we found it is because regex

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

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

4 Comments

+1, Great Hack :) Could you please explain the reason for this behavior? Your Source?
namespace part is regex, so if you don want to trigger click to not namespaced event only , you have to find a way to set empty namespace string here.
Is there any way to trigger every click handler except one with the namespace confirm? ('click.confirm') I don´t really understand regex very well..

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.