5
<div id="conversations-uCount">0</div>
<script type="text/javascript">
$(document).ready(function() {
    $('#conversations-uCount').data('UnreadIDs', '1');
});
</script>

How can I set a bind so that any time the UnreadIDs changes I can run a function?

Thanks

Sampson
  • 259,174
  • 73
  • 529
  • 557
AnApprentice
  • 103,298
  • 185
  • 610
  • 989

3 Answers3

4

In jQuery 1.4.4+ there's an event triggered for this, changeData. If that's the only data you're dealing with on the object, your handler is as simple as:

$('#conversations-uCount').bind("changeData", function() {
  //data changed, do something, for example:
  alert("Data changed!, new value for UnreadIDs: " + $.data(this, 'UnreadIDs'));
});

You can test it out here.

Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151
1

I imagine you could do that with a simple plugin:

$.fn.dataTrigger = function(name, value, callback) {
    $(this).data(name, value);
    callback(name, value);
    return this;
};

Then:

$('#conversations-uCount').dataTrigger('UnreadIDs', '1', myFunc);

Demo: http://jsfiddle.net/karim79/q6apA/1/

karim79
  • 334,458
  • 66
  • 409
  • 405
0

Yes there is a function called watch object.watch but its not standard, I think what your looking for can be found here Object.watch() for all browsers?

Community
  • 1
  • 1
Amjad Masad
  • 3,985
  • 1
  • 19
  • 20