Home  >  jQuery

Example of .data() in jQuery

April 29, 2013
.data() in jQuery works as collection. We can store data as key value pair. .data() also returns value fetched by key. Syntax is
data( key, value )
data( key )
 
Find the example.

<!DOCTYPE html>
<html>
<head>
<style>
div { color:green; }
p { color:blue; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div>
Hi, this is 
<p></p>
here, we learn a lot
<p></p>
</div>
<script>
$("div").data("datatest", { a: "concretepage.com",b:"in jQuery" });
$("p:first").text($("div").data("datatest").a);
$("p:last").text($("div").data("datatest").b);
</script>
</body>
</html>
 




Output








©2021 concretepage.com | Privacy Policy | Contact Us