Martin
Martin

Reputation: 233

Super basic JQuery If function //

I'm trying to hide a div, based on whether another div has a class.

I can't give the class/classless div an ID -- since everything is sitting on hidden templates that I' can't get to.

Seems like I should be able to say if this div has a class, do xx to this other div.

I'm sure I'm making a super basic syntax mistake, but all the parentheses seem like they're in the right place.

Here's what I've got:

if ( $('div').hasClass( '.test-class' ) ) {

 $( ".to-hide" ).css("display", "none");

}

http://jsfiddle.net/HL93f/3/

Embarrassed thanks, as always.

//Updated// Thanks!

Upvotes: 0

Views: 41

Answers (3)

Murali Murugesan
Murali Murugesan

Reputation: 22619

Remove . in your class

if ( $('div').hasClass( 'test-class' ) )

Upvotes: 0

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

$('div').hasClass( 'test-class' )//no dots

http://jsfiddle.net/HL93f/5/

Upvotes: 1

kinghfb
kinghfb

Reputation: 1021

You don't need the dot for the hasClass() function

Upvotes: 1

Related Questions