jQuery data() Attach data to element via object

Description

jQuery data() Attach data to element via object

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  testObj = new Object();
  testObj.a = "aaa!";
  testObj.b = "bbb!";
  $("#btn1").click(function(){
    $("div").data(testObj);
  });/*from  w ww.  ja  va  2  s  . c o m*/
  $("#btn2").click(function(){
    document.getElementById("demo").innerHTML = $("div").data("a");
  });
});
</script>
</head>
<body>

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

<div></div>

</body>
</html>



PreviousNext

Related