Using plain JavaScript (not jQuery), Is there any way to check if an element contains a class?
Currently, I\'m doing this:
in which element is currently the class '.bar' ? Here is another solution but it's up to you.
var reg = /Image/g, // regexp for an image element
query = document.querySelector('.bar'); // returns [object HTMLImageElement]
query += this.toString(); // turns object into a string
if (query.match(reg)) { // checks if it matches
alert('the class .bar is attached to the following Element:\n' + query);
}
jsfiddle demo
Of course this is only a lookup for 1 simple element
(/Image/g
) but you can put all in an array like is
/LI/g
,
= /UL/g
etc.