• All geek questions in one place

    Add class to html tag using jquery

    I got this html code:

    <form id = "xxx">
       <ul>
         <li class = "req" > </li>
         <li class = "req" > </li>
         <li class = "req" > </li>
         <li > </li>    ==================> HOW CAN I ADD A CLASS="req" to this li with jquery
         <li class = "req" > </li>
         ....
        <li class = "req" > </li>
      </ul>
    </form>
    

    So, I rewrite the question: is there an li tag that does not have (class = "req"), how can I add this using jquery? Thanks you

    +3
    jquery html
    Mamadou 20 sept '10 at 9:47
    source share
    3 answers
    $("#xxx li").addClass("req");
    

    .

    If you want to explicitly add it only to liwithout req, you can do:

    $("#xxx li:not(.req)").addClass("req");
    

    If you want to add a class only to the fourth element, follow these steps:

    $("#xxx li:eq(3)").addClass("req");
    

    3lies in the fact that it uses a zero index for elements.

    +4
    Magnar 20 sept '10 at 9:49
    source share

    If you cannot check if the element has a specific attr.

    $("ul#yourid").not(".req").addClas("req");
    
    +1
    Tim 20 sept '10 at 9:50
    source share

    jQuery('li').addClass('req');

    0
    Randy the dev 20 sept '10 at 9:49
    source share

    Source: https://habr.com/ru/post/1765595/

    More articles:

    • Function Modifier Order in C # - syntax
    • Служба WCF - Пользовательский директор - wcf
    • Как сохранить соотношение в одной переменной и прочитать его в С# - c#
    • How to choose ORM - php
    • R, replace NA with zero - r
    • How to remove "Cant find PInvoke dll SQLite.interop.dll" error - sqlite
    • Can I prevent a digital signature warning when starting a Java application from the command line? - java
    • How to get Mario Wii Map Points after a race to the database - mysql
    • How to add distance between two span elements - html
    • How to force binding in Emacs? - emacs

    All Articles

    Geek Questions | 2019