Skip to content

Instantly share code, notes, and snippets.

@MSerj
Created December 20, 2017 16:57
Show Gist options
  • Save MSerj/bc503554f81899fb005a12ced975eb13 to your computer and use it in GitHub Desktop.
Save MSerj/bc503554f81899fb005a12ced975eb13 to your computer and use it in GitHub Desktop.
get class list of element
//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
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment