1

I have the following html codes:

    <li class="imagenav active">
      some codes go here
    </li>

Is there a way of checking if imagenav class is active or change CSS class using Javascript. for example:

    if imagenav is active {
      do some codes here ...
    }

Many thanks in advance.

Thank you for Barmar for advising how to check if a list has a class. However, in my case, I prefer to change CSS class using Javascript which makes it easier to manipulate my codes. So I have to add id into HTML codes. I want the left arrow to be active/disabled when the page is first loaded. Here are my fully working codes:

    <div id="active-leftarrow" class="imagenav">
      <script type='text/javascript'>
        document.getElementById('active-leftarrow').className = 'imagenav disabled';
      </script>
    </div>

Many thanks

6
  • 3
    In JavaScript you can use if (element.classList.includes("active")) Commented Jul 30, 2024 at 23:29
  • 3
    In jQuery, if ($(".imagenav").hasClass("active")) Commented Jul 30, 2024 at 23:31
  • but that does not have a specific imagenav class; how would javascript know it refers to imagenav? Commented Jul 30, 2024 at 23:31
  • 2
    I assumed you set element first to the imagenav element. let element = document.querySelector(".imagenav") Commented Jul 30, 2024 at 23:32
  • Thanks I ll try that and give a feedback sortly. Commented Jul 30, 2024 at 23:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.