Use .data() to set data attribute - Javascript jQuery

Javascript examples for jQuery:Data Attribute

Description

Use .data() to set data attribute

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/* w w w.java2s.c om*/
$(function(){
    $('a').click(function(){
        var txt= $(this).text();
        $('div').data('key',txt)
        $('span').text($('div').data('key'))
    })
})
    });

      </script> 
 </head> 
 <body> 
  <div></div> 
  <a href="#">My text</a> 
  <span></span>  
 </body>
</html>

Related Tutorials