Hello Readers,
jquery.data() method or we call it as $.data is the jquery method and it is used in this DOM element associate with the data and the JavaScript objects. And, this method is used to make the jquery code clear and concise.
Syntax :
jQuery.data( element, key, value )
Parameters :
element : This element is the DOM element to associate with the data.
key : key is the type of string where the piece of data to set.
value : this can be any JavaScript type except undefined.
Code Example:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.data demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>
The values stored were
<span></span>
and
<span></span>
</div>
<script>
var div = $( "div" )[ 0 ];
jQuery.data( div, "test", {
first: 16,
last: "pizza"
});
$( "span:first" ).text( jQuery.data( div, "test" ).first );
$( "span:last" ).text( jQuery.data( div, "test" ).last );
</script>
</body>
</html>
Output :
The values stored were 16 and pizza
0 Comment(s)