Add items in the DOM [closed]

1

Hi, I need to add a different class attribute for each label to a li but I do not know how to do it. You can help me would be with jquery

    
asked by beaag58 14.05.2018 в 09:44
source

3 answers

0

after the append that generates the li

li.setAttribute("class", "nombredelaclase");
    
answered by 14.05.2018 в 09:54
0

If you want to add a class, but the same to all "a" in "li" you do not need to loop, you can simply do

$("li a").addClass("laclase");
    
answered by 14.05.2018 в 10:50
0

You can do it this way using jQuery . Take into account that your array of classes must be the same size as your number of elements li :

var classes = ['classone', 'classtwo', 'classthree'];
var i = 0;        
$('#milista').find('li').each(function(){
    let $element = $(this);
    $element.attr('class',classes[i]);
    i++;
});
.classone{
 color: red;
}
.classtwo{
 color: blue;
}
.classthree{
 color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id = "milista">
    <li>Uno</li>
    <li>Dos</li>
    <li>Tres</li>
</ul>
    
answered by 14.05.2018 в 10:11