user120944
user120944

Reputation:

Simple jQuery If then

Losing my mind on this one..

How can I do something as simple as..

If div has class "something" then make div#test have a class of "awesome"

<div class="something">hey there</div>

<div id="test">Am I awesome?</div>

Much Thanks!

Upvotes: 1

Views: 137

Answers (3)

Inversus
Inversus

Reputation: 3175

jQuery has a .hasClass() method that is useful, albeit maybe not exactly what you are looking for.

hasClass()

Upvotes: 0

Tom Leys
Tom Leys

Reputation: 19029

If you want to check the class of a particular div, then change your html

<div id="im_special" class="something">hey there</div>

and then look for the id in the javascript

if ($("div#im_special.something").length) {
    $("div#test").addClass("awesome");
}

Upvotes: 0

Gumbo
Gumbo

Reputation: 655785

Try this:

if ($("div.something").length) {
    $("div#test").addClass("awesome");
}

Upvotes: 2

Related Questions