1

I've done my research and found .hasClass() and .is() is similar to what I'm needing. However, it doesn't work well in my case. I'm trying to check a DIV to see if it has a specific class after page load. This continually changes as it's based on if the user scrolls..if the user scrolls, jQuery then adds a class (.ctoggle) to .first-div, from there I want it to "automagically" add a class to the second (.active) and toggle if removed from .firstdiv.

Here's an example if user has scrolled:

<div class="first-div ctoggle"></div>

<div class="shade active"></div>

. Any ideas on how can I achieve this? Again, correct me if I'm wrong but seems as though .hasClass() and .is() only works before/during page load.

2
  • 2
    hasClass and is work when you call them, at any time. But I don't get what you try to do. Nov 10, 2012 at 16:17
  • Use setTimeout() to run a function every XXX seconds. Nov 10, 2012 at 17:56

1 Answer 1

2

.hasClass() and .is() work any time you call them as long as your target object exists. But, they don't continue to report to you any time the class might change - they don't monitor for changes.

If you want to check an object every time the page is scrolled to see if its class has changed, then you just need to hook up some code to either the scroll event or to the code that is modifying the page on the scroll event and check the object every time that event fires. It is up to you to write code that determines when you need to check the target object again.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.