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

Adam Link
  • 2,783
  • 4
  • 30
  • 44
voigtan
  • 8,953
  • 2
  • 29
  • 30
  • Can you be more specific on what is not working? – Selvakumar Arumugam Dec 03 '12 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. – voigtan Dec 03 '12 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. – Kevin B Dec 03 '12 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. – voigtan Dec 03 '12 at 21:04
  • http://jsfiddle.net/oceog/RczDZ/5/ check this fiddle and tell what is expected result after one click – zb' Dec 03 '12 at 21:05
  • @eicto I expect to get the events with no namespace to be trigged when I use `event!` code but its been removed in: https://github.com/jquery/jquery/commit/395f1da76ba9faeb2f72548c28da228474a2434c so my question is more or less, is the second example the way to go to keep the code above to keep working. – voigtan Dec 03 '12 at 21:10
  • tell me what output you expecting, be simple – zb' Dec 03 '12 at 21:11
  • [same example with output i expirencing](http://jsfiddle.net/oceog/RczDZ/8/) I expect exactly same result, do you expect other ? what is ? – zb' Dec 03 '12 at 21:14
  • I edited my main code... I want to get the message of: "click without a namespace" on my main example. the main question is, is this the only way to go after 1.8.x of jquery? – voigtan Dec 03 '12 at 21:15
  • which browser you use ? I getting this message in your example, on start, and both "with namespace" "without namespace" on click, seems correct for me – zb' Dec 03 '12 at 21:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20532/discussion-between-voigtan-and-eicto) – voigtan Dec 03 '12 at 21:20

1 Answers1

12

You can call that using

.trigger("click.$")

as we found it is because regex

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

zb'
  • 8,071
  • 4
  • 41
  • 68
  • +1, Great Hack :) Could you please explain the reason for this behavior? Your Source? – Starx May 09 '13 at 10:32
  • 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. – zb' May 09 '13 at 10:44
  • 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.. – Mamut Aug 24 '16 at 09:37
  • 1
    @Mamut http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word – zb' Aug 24 '16 at 11:39