CoryDorning
CoryDorning

Reputation: 1914

jQuery data() and 'changeData' event

I can add a click event to an HTML element and have the click event update $(document).data(key,value).

This would then trigger $(document).bind('changeData', function() {...})). Knowing this, what would be the best way to find out which HTML element updated $(document).data()?

E.g.:

$(document).bind('changeData', function {
  // get the anchor that set $(document).data()
});

$('a').click(function() {
  $(document).data('key', 'value');
});

Upvotes: 7

Views: 8993

Answers (2)

TankorSmash
TankorSmash

Reputation: 12777

https://bugs.jquery.com/ticket/11718

jQuery's setData and changeData events have been deprecated since 1.8 and removed in 1.9. This means that in 2.X and 3.X you're no longer able to bind to these events.

Upvotes: 3

Robert Ross
Robert Ross

Reputation: 1925

Well for the data by click event:

$('#elementSelector').click(function(e){
    $(document).data(key, val);
});

$(document).bind('changeData', function(e){
    // e.target?
});

If not, try storing the target within the data stored:

$('#elementSelector').click(function(e){
    $(document).data(key, {
        val: 'value',
        target: e.target
    });
});

Then when you have the change event:

$(document).bind('changeData', function(e){
    var data = $(this).data(key);
    var target = data.target;
    var value  = data.val;
});

Upvotes: 3

Related Questions