I don't want to know if an element contains a specific class, but just if it's loaded in DOM:
$(myObject).attr("class").length;
or
if($t.classList.length) {
var classSUP = $t.attr("class");
} else {
var classSUP = $t.attr("id");
};
The easiest way to check would be:
if($("element").attr("class")) {
return true;
}
Example shown here: http://jsfiddle.net/Skooljester/XpUJA/
return true; so that it could be replaced with whatever.hasAttr() is not part of the standard jQuery API.Try this:
var containsClass = $t.attr("class") !== "" && $t.attr("class") !== undefined;
Here is as a function:
function containsClass($t)
{
return $t.attr("class") !== "" && $t.attr("class") !== undefined;
}
.attr('class') will return undefined for these poor classless elements.removeAttr, too. But those suffering from removeClass will return an empty string in this case.removeClass will have a class attribute of an empty string, so it's safer to use my method because an empty string is not equal to undefined.
varprefixes after the first one are just ignored.vardeclarations will be hoisted (one of them will result in creating theclassUPvariable, the other will be ignored, as @raina77ow noted). But the assignments are statements, not declarations and will not be hoisted. They will execute at that exact line of code where they are written.