How can I add a class to a span element for only these span
s which are part of li
tags with class='error'
per example, this is the HTML which is generated:
<li class="error">
<!--if list has class error, then add also class error to span which is part of that list tag-->
<span class="error"></span>
</li>
This is the code I have so far:
if(status == 'error') {
data.context.addClass('error'); // adds class error to li tag
}
For a pure vanilla JavaScript solution, you can use an attribute selector to find all your
li
elements that have a class callederror
. Once you have that list ofli
elements, just loop thru (usingforEach
or a for-loop) and append an aspan
element as a child of theli
. The span element should also be given a class attribute called “error” viasetAttribute
.This should achieve the expected result. Iterate through all li elements with the
error
class and find any spans, then add the classerror
to it.Instead of using loops you can do it like this-
This is easy using JQuery, just select all
li
items that have the class.error
and then use find() to find all thespam
elements inside it, finally add the class.error
to those:Easiest way-