1

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");
};
4
  • side note - your two var declarations will be hoisted and overwrite each other. you can't declare variables in a block in javascript. Commented Jul 9, 2012 at 15:31
  • 2
    @jbabey I'm sorry, what? All successive var prefixes after the first one are just ignored. Commented Jul 9, 2012 at 15:36
  • @jbabey Yes, the var declarations will be hoisted (one of them will result in creating the classUP variable, 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. Commented Jul 10, 2012 at 11:15
  • @Imp yes thank you for clarifying. what i meant to say is that you should not write code that will be interpreted differently than you wrote it; it will lead to bugs in the future. you should declare variables outside of the condition and then just assign in the condition. Commented Jul 10, 2012 at 12:40

5 Answers 5

1

Or how about just

(myObject.className != '')

or, just to be sure about possible additional spaces

(myObject.className.replace(' ', '') != '')  

This is no job for a framework ;)

Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way to check would be:

if($("element").attr("class")) {
   return true;
}

Example shown here: http://jsfiddle.net/Skooljester/XpUJA/

4 Comments

@raina77ow I didn't know what OP wanted to do with it, so I just put return true; so that it could be replaced with whatever.
hasAttr() is not part of the standard jQuery API.
@jmoerdyk You're right, not sure how I've used that before then. Changed it so that it's correct.
thank's but i'm sure i have try this without results, maybe a bug ?
1

You don't need jQuery to do this:

if (element.className) {
    // element has a class
}

or, if you really want to use jQuery:

if ($('#elementID').attr('class')) {
    // element has a class
}

Comments

1

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;
}

8 Comments

More precise, .attr('class') will return undefined for these poor classless elements.
... and for those that have their classes detached by removeAttr, too. But those suffering from removeClass will return an empty string in this case.
thank's a lot to everybody, it was very simple, i solve it by : if($t.attr('class') != undefined ){xxxxxxxx}
@gaelboyenval - As raina77ow said, elements who have their classes removed by 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.
@jeff how to ask ? if(containsClass){} ? i don't know how to do
|
0

The easiest way to detect if particular object loaded in DOM is:

if ( $('.className').length ) {
     alert('.className is on DOM')
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.