Tuesday, June 16, 2015

Pass data via attribute in JavaScript




 We create a data attribute sid (you can change to any name) and assign it value 3456
<tr><td class='sid' id='firstname' data-sid='3456'></td> </tr>
Below is the script to click this cell to display this value using jQuery:
     $('.sid').on('click',  function(){
                     var $tr = $(this).parents('tr');
                     var sid = $tr.find('#firstname').attr('data-sid');

                    alert(sid);
        });

No comments:

Post a Comment