1

If I replace the content of a span using the syntax below, does it raise an event?:

$("#container").html(data);

I am trying to refresh another div whenever the content of #container changes, but I don't know which #container event to bind. I am using jQuery.

Any assistance appreciated.

UPDATE: Here is a more comprehensive code listing...

$.post(addUri, myForm.serialize(), function(data) {
       $("#container").html(data);
   }, 'html');
DatsunBing
  • 8,684
  • 17
  • 87
  • 172
  • Why can't you refresh the div everytime you insert something into `#container`? Is `#container` mutated by an external script? – Sahil Muthoo Oct 13 '11 at 11:25
  • I have updated with a more comprehensive code listing. Basically, if I call my refresh function after the post, the span has not had time to update. I could make the post synchronous, but I don't really want to do that... – DatsunBing Oct 13 '11 at 11:30

3 Answers3

2

I don't think there's a built-in listener for content changes in a span, so you could create your own:

$('#container').bind('contentchanged', function() {
  alert('the content of my container changed!');
});

and trigger it like this:

$("#container").html(data).trigger('contentchanged');
Hoff
  • 38,776
  • 17
  • 74
  • 99
0

You could use .trigger() to trigger a custom event and listen for that with .bind().

njr101
  • 9,499
  • 7
  • 39
  • 56
0

jQuery doesn't provide the functionality to track changes to content. Someone has written a plug-in to do what you need in the answers to this question - jQuery watch div

Community
  • 1
  • 1
ipr101
  • 24,096
  • 8
  • 59
  • 61