Is it possible to execute a script only inside an element?
For example I have a list of <div> elements with two <span> inside of each element. I check with JS if any div has a class called "boo" assigned to it. If it does, it should add a class "hide" to the <span> with the class "child2", if not, it should add the class "hide" to the other <span> with the class "child1".
Here's an example of what I mean.
<div> <span class="child1"></span><span class="child2"></span> </div>
<div class="boo"> <span class="child1"></span><span class="child2"></span> </div>
If I'd now use a script like the following, it would mess up the whole list, because it would also add the class "hide" to the other elements. How can I solve this?
var check = $("div").hasClass("boo");
if (check === true) {
$( ".child2" ).addClass("hide");
}
else {
$( ".child1" ).addClass("hide");
}