Articles :: View

Get element class names in array using JavaScript, jQuery is overkill

http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery

You can use document.getElementById('divId').className.split(/\s+/); to get you an array of class names.

Then you can iterate and find the one you want.

var classList = document.getElementById('divId').className.split(/\s+/);
for (var i = 0; i < classList.length; i++) {
   if (classList[i] === 'someClass') {
     //do something
   }
}
jQuery does not really help you here.....
var classList =$('#divId').attr('class').split(/\s+/);
$.each( classList, function(index, item){
    if (item === 'someClass') {
       //do something
    }
});


Comments

Be the first to add a comment!