-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?

3
  • 4
    What have you tried? A quick Google search produces plenty of results. Commented Aug 21, 2015 at 9:37
  • 2
    .hasClass() may help you! Commented Aug 21, 2015 at 9:38
  • you should go through this api.jquery.com/hasclass Commented Aug 21, 2015 at 9:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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{}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.