-4

This is how I have my "ul" written. As you can see there are two classes written in for the ul

<ul class="nav-second-level collapse">
</ul>

Now there may be another addition of a class to the same div like below

  <ul class="nav-second-level collapse in">
    </ul>

I need to check whether the class "in" is present in the "ul". How can I do that using jquery/javascript?

Arun Mohan
  • 898
  • 3
  • 18
  • 37

2 Answers2

1

With jQuery you can use jQElement.hasClass();

Without jQuery, you can use element.className or element.classList.

the first will produce a string like "nav-second-level collapse" and the second will produce an Array of strings ["nav-second-level", "collapse"].

And then, you can check if either the string or the array contains your class, with .indexOf(yourClass)

However, classList is more recent and you should check the compatibility table before using it.

Lauromine
  • 1,453
  • 9
  • 19
0

just check that class by

    var test = $(ul).hasClass( "in" ); 

this will return "true".

you can then put if condition

    Ex: if(test == true){}else{}