How to add a class to html tag?

I have an HTML code like

<div class="class1 class2">
  <ul><li class="class1"></li></ul>
</div>

Here the div tag and the li tag have same class name, I just want to add another class to the li tag that have the class class1. How can I do this?

Answers 1

  • First of all your markup is not valid

    The HTML <li> element (or HTML List Item Element) is used to represent an item in a list. It must be contained in a parent element: an ordered list (<ol>), an unordered list (<ul>), or a menu (<menu>). ( Taken from : https://developer.mozilla.org/en/docs/Web/HTML/Element/li )


    You can add class by using addClass()

    $('li.class1').addClass('class2');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="class1">
      <ul><li class="class1"></li></ul>
    </div>


Related Articles