Created
December 2, 2015 00:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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