Hello Readers,
jQuery data method is usually used to set and get data functionality.
Attach data to a HTML tag(like div or span) element, then retrieve the data.
The jQuery data() method attaches data to, or gets data from, selected elements.
Syntax: $(selector).data(name)
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#setBtn1").click(function(){
alert($(".setData").data("getSetKey"));
});
$("#setBtn2").click(function(){
alert($(".setData").data("getSetKey","This is the value to print"));
});
});
</script>
</head>
<body>
<button id="setBtn1">First attach data to div element</button><br>
<button id="setBtn2">Second get data attached to div class (setData) element</button>
<div class=".setData"></div>
</body>
</html>
Thanks
0 Comment(s)