Wednesday 18 December 2019

javascript - Get class name using jQuery



I want to get the class name using jQuery



And if it has an id






Answer



After getting the element as jQuery object via other means than its class, then



var className = $('#sidebar div:eq(14)').attr('class');


should do the trick. For the ID use .attr('id').




If you are inside an event handler or other jQuery method, where the element is the pure DOM node without wrapper, you can use:



this.className // for classes, and
this.id // for IDs


Both are standard DOM methods and well supported in all browsers.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...