65

I created an attribute in HTML data-select-content-val and it is stuffed with information dynamically.

Is there a way to detect when the attribute's value has changed?

$(document).on("change", "div[data-select-content-val]", function(){
    alert("BOOP!");
});
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Majo0od
  • 2,278
  • 10
  • 33
  • 58

3 Answers3

52

You would have to watch the DOM node changes. There is an API called MutationObserver, but it looks like the support for it is very limited. This SO answer has a link to the status of the API, but it seems like there is no support for it in IE or Opera so far.

One way you could get around this problem is to have the part of the code that modifies the data-select-content-val attribute dispatch an event that you can listen to.

For example, see: http://jsbin.com/arucuc/3/edit on how to tie it together.

The code here is

$(function() {  
  // Here you register for the event and do whatever you need to do.
  $(document).on('data-attribute-changed', function() {
    var data = $('#contains-data').data('mydata');
    alert('Data changed to: ' + data);
  });

  $('#button').click(function() {
    $('#contains-data').data('mydata', 'foo');
    // Whenever you change the attribute you will user the .trigger
    // method. The name of the event is arbitrary
    $(document).trigger('data-attribute-changed');
  });

   $('#getbutton').click(function() {
    var data = $('#contains-data').data('mydata');
    alert('Data is: ' + data);
  });
});
Community
  • 1
  • 1
martineno
  • 2,623
  • 17
  • 14
  • 20
    Wait so your answer is don't detect but rather trigger an event when it on a click? Hmmm – ChristoKiwi Oct 20 '16 at 02:36
  • 1
    i wonder why the designers of HTML decided not to support "onattributechange". It is possible to create your own event model on top of HTML, but if it is done, then you need to prohibit direct manipulation of DOM without issuing an event alongside it(eg, if you change checked property of a checkbox, you must also issue an event that you changed the checked property to the window), allowing the event model to behave correctly, but this breaks a lot of existing modules that depend on direct dom mutation, and leaves room of difficult to find bugs due to forgetting to issue an event.. – Dmytro May 26 '18 at 20:39
28

You can use MutationObserver to track attribute changes including data-* changes. For example:

var foo = document.getElementById('foo');

var observer = new MutationObserver(function(mutations) {
  console.log('data-select-content-val changed');
});
observer.observe(foo, { 
  attributes: true, 
  attributeFilter: ['data-select-content-val'] });

foo.dataset.selectContentVal = 1;
 <div id='foo'></div>
 
nkron
  • 19,086
  • 3
  • 39
  • 27
11

There is this extensions that adds an event listener to attribute changes.

Usage:

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript"
  src="https://cdn.rawgit.com/meetselva/attrchange/master/js/attrchange.js"></script>

Bind attrchange handler function to selected elements

$(selector).attrchange({
    trackValues: true, /* Default to false, if set to true the event object is 
                updated with old and new value.*/
    callback: function (event) { 
        //event               - event object
        //event.attributeName - Name of the attribute modified
        //event.oldValue      - Previous value of the modified attribute
        //event.newValue      - New value of the modified attribute
        //Triggered when the selected elements attribute is added/updated/removed
    }        
});
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632