Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery "hasClass()" not working?

I need to hide a div if another div has a class.

I've created a very basic example HERE

I need the div on the bottom to hide when the word "click" is.. well.. clicked. It adds a class to the middle div just fine, but it seems hasClass() doesn't want to work?

NOTE: The structure needs to be like this. If "click" is clicked, modify the middle div (add class?), and manipulate the bottom div based on the middle div. With this setup - I can't just do "if CLICK is clicked, slideUp() the bottom div".

Also, once "ok" or "cancel" is clicked, it will revert, because the middle div will no longer have the class. Provided that's the method I can get working here, haha.

like image 288
Xhynk Avatar asked Oct 26 '12 19:10

Xhynk


2 Answers

On a side note, another variation to check if there's a class is:

if ( $('body.className').length ) {

Still recommend hasClass though. Just nice to see variation sometimes.

like image 54
Jack Avatar answered Oct 13 '22 01:10

Jack


your if statement is outside of any function, so there is no reason for it to be called after the script is loaded.

See this fiddle, I think that's what you want.

like image 29
EyalAr Avatar answered Oct 12 '22 23:10

EyalAr