0

Given the following HTML:

<div id="foo"> <p>hello world</p> </div> <p id="message">Event not yet triggered</p> 

And the following JS:

$('#foo').bind('customEvent', function() { $('#message').text('Event triggered!'); }); $.event.trigger('customEvent'); 

My p tag is changed in jQuery 1.8 (JSFiddle), but not in 1.9 (JSFiddle).

Why is this? I can't see anything relevant in the changelog for 1.9. What's the best approach to use in 1.9+?

2
  • 2
    Should be $('#foo').trigger('customEvent'); as thats the element you bound the event to Commented Apr 16, 2014 at 11:33
  • 1
    +1 to what @adeneo said, and also ... you're on jQuery 1.9, you should be using .on() instead of .bind(). Commented Apr 16, 2014 at 11:34

1 Answer 1

1

If you want a global style event you can trigger events on the document body. Triggering events on more specific elements can be more efficient but this may not be what you want.

$(document.body).trigger('customEvent'); 

See: http://jsfiddle.net/7c8M7/

An alternative approach is to use a dedicated pub-sub library like: https://github.com/mroderick/PubSubJS

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.