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
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
after the append that generates the li
li.setAttribute("class", "nombredelaclase");
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");
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>