Getting id and class value of a html element
We have a div and we want to get its id value based on its class value. Suppose a div is given as below and we want to fetch div id based on a button click. for getting attribute id we use attr() method. Example
1 2 3 4 5 | <div class="select" id="select_25"> This is a ball </div> <input type="submit" value="Get Id" id="submit"> |
1 2 3 4 5 6 7 | $(document).ready(function () { $(document).on('click', '#submit', function (event) { event.preventDefault(); alert("id is "+$(".select").attr('id')); alert("class is "+$("#select_25").attr('class')); }); }); |
Output