Wednesday, June 3, 2015

jquery to obtain the ID or to use the data.

To access data/id from html

in html form let say it's a button, we can put any fields as the data (by putting the tag data-<something>)

<input class="myclass" data-myvalue="myValue123" id="myID" type="button" />

To access the element using ID from the javascript:
$('#myID').click(function(e){
   var myvalue = $(this).data('myvalue');
});

To access the element using the class from javascript:
$('.myclass').click(function(e){
   var myvalue = $(this).data('myvalue');
   var myID = $(this).attr('id');
});


No comments: