jQuery Get Data Atribute Value

Gets the value of the data attribute in a html tag

If your html tag looks like this:

var this_data = $(this).attr('data-my-data');

I like to use these in click functions


<script> 
 
$(document).ready(function() { //12PM DOC READY
          
  $( ".mybutton" ).click(function() {
    var this_data = $(this).attr('data-my-data');
    alert(this_data);
  });
 
}); //12PM DOC READY
 
</script>

This will alert “some cool data”

Comments