Created
September 29, 2014 22:50
-
-
Save bbatsche/ac5d5b161294409c59b4 to your computer and use it in GitHub Desktop.
Demonstrate getting & setting data attributes of an element
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>JQuery, YAY!</title> | |
</head> | |
<body> | |
<h1 id="heading" data-alt-heading="MAGIC">Example Page</h1> | |
<button type="button" id="swap-heading">Do The Thing!</button> | |
<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script> | |
<script> | |
$(document).ready(function() { | |
$("#swap-heading").click(function() { | |
// grab the value of "data-alt-heading" | |
var newHeading = $("#heading").data('alt-heading'); | |
// grab the current HTML content | |
var currentHeading = $("#heading").html(); | |
// save the current HTML content in the data store | |
$("#heading").data('alt-heading', currentHeading); | |
// set the element HTML to the new heading | |
$("#heading").html(newHeading); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment