Attach data to selected elements using an object with name/value pairs. - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:data

Description

Attach data to selected elements using an object with name/value pairs.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    testObj = new Object();
    testObj.a = "a!";
    testObj.b = "b!";
    $("#btn1").click(function(){
        $("div").data(testObj);
    });// w  ww  . j  a  v  a2  s  .co m
    $("#btn2").click(function(){
        console.log($("div").data("a"));
    });
});
</script>
</head>
<body>

<button id="btn1">Attach data to div element</button><br>
<button id="btn2">Get data attached to div element</button>

<div></div>

</body>
</html>

Related Tutorials