Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery data() and 'changeData' event

Tags:

jquery

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');
});
like image 234
CoryDorning Avatar asked Mar 23 '11 19:03

CoryDorning


People also ask

What is the change event in HTML?

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until...

Why can't I bind to setdata and changedata events?

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. Thanks for contributing an answer to Stack Overflow!

Why does the change event display twice in jQuery?

The message will display twice, because the handler has been bound to the change event on both of the form elements. As of jQuery 1.4, the change event bubbles in Internet Explorer, behaving consistently with the event in other modern browsers.

What is the event data property in JavaScript?

The event.data property contains the optional data passed to an event method when the current executing handler is bound. Required. The event parameter comes from the event binding function


2 Answers

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;
});
like image 86
Robert Ross Avatar answered Oct 04 '22 15:10

Robert Ross


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.

like image 33
TankorSmash Avatar answered Oct 04 '22 15:10

TankorSmash