CoryDorning CoryDorning - 3 months ago 9
jQuery Question

jQuery data() and 'changeData' event

I'm trying to figure something out. I can add a

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

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

Any help is appreciated.

Ex.

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

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

Answer

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;
});