Detecting attribute change of value of an attribute I made

JqueryHtml

Jquery Problem Overview


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

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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>
 

Solution 3 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMajo0odView Question on Stackoverflow
Solution 1 - JquerymartinenoView Answer on Stackoverflow
Solution 2 - JquerynkronView Answer on Stackoverflow
Solution 3 - JqueryShimmy WeitzhandlerView Answer on Stackoverflow