2

I have an LI which I'm adding classes to dynamically.

I need to check if the LI has a certain class that was inserted dynamically and insert another class into it.

How is is this possible?

More explanation: After DOM loads the LI looks like this:

<li class="active"></li>

After dynamically inserting a class the LI looks like this:

<li class="active correct"></li>

Now I need to check if the LI contains the class "correct"

zk_mars
  • 1,339
  • 2
  • 15
  • 36
Richard
  • 1,414
  • 2
  • 16
  • 27
  • 2
    $(selector).hasClass("class") – Dimitri Dec 17 '13 at 19:10
  • @Dimitri I tried that, it only checks after the DOM loads initially, not after the class was inserted dynamically. – Richard Dec 17 '13 at 19:13
  • Sorry but the question still seems a little unclear. When are you adding the class? It sounds like your looking for some event that fires when a new class is added/removed if I understand correctly. Unfortunately I do not think there is such a thing. – War10ck Dec 17 '13 at 19:14
  • you have to check after it gets added. – Venkata Krishna Dec 17 '13 at 19:14
  • @Richard, shouldn't matter when you insert class. hasClass will always check current state at the time of invocation. May be your invocation is not timed right. Please show some more code. – Dimitri Dec 17 '13 at 19:14
  • @Dimitri ok thanks for that, I'll have to recheck the code then. – Richard Dec 17 '13 at 19:14
  • Have you tried `window.onload` instead of `document.ready`? – Cholesterol Dec 17 '13 at 19:25

1 Answers1

0

You can use .hasClass() to check whether the selected element is having a particular class or not. It doesn't matter when you are adding those classes. Dynamically added classes would also deliver same result with .hasClass().

Try,

$('li').hasClass('correct')
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130