Skip to content

Instantly share code, notes, and snippets.

@brschwar
Created December 2, 2015 00:03
Show Gist options
  • Save brschwar/bfb29316548b67759a31 to your computer and use it in GitHub Desktop.
Save brschwar/bfb29316548b67759a31 to your computer and use it in GitHub Desktop.
Detect when an element's attribute value has changed (like mutation observer): example for 'data-select-content-val' that is updated with information dynamically.
$(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);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment