1

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");
                  }
0

2 Answers 2

6

The issue is because your code is looking at the first div only, and all the .child1 and .child2 elements.

To fix this amend your selector to select by the presence (or lack of) the .boo class:

$('div.boo .child2').addClass('hide');
$('div:not(.boo) .child1').addClass('hide');
.hide { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div> 
  No boo
  <span class="child1">
    Child 1
    <span class="child2">Child 2</span> 
  </span>
</div>
<div class="boo">   
  Boo
  <span class="child1">
    Child 1
    <span class="child2">Child 2</span> 
  </span>
</div>

Also note that if this is being done only for UI reasons then you should not be using JS at all. It can be achieved in CSS alone:

div:not(.boo) .child1,
div.boo .child2 {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  No boo
  <span class="child1">
    Child 1
    <span class="child2">Child 2</span>
  </span>
</div>
<div class="boo">
  Boo
  <span class="child1">
    Child 1
    <span class="child2">Child 2</span>
  </span>
</div>

Finally note that your span elements are missing a closing tag. I presume this is just a typo in the question so I corrected it in this example.

Sign up to request clarification or add additional context in comments.

1 Comment

oh thanks, it took me so long testing everything that I lost the easy solution. Thank you!
1

You don't need jQuery for this:

let children = document.querySelectorAll('div.boo .child2, div:not(.boo) .child1');

for (var j = 0; j < children.length; j++) {
  children[j].classList.add("hide");
}

You also miss some closing span tags in your html, I'm assuming is just a typo.

Also, as stated above, if it's just a matter of presentation you can accomplish this with simple css.

1 Comment

If you're avoiding jQuery then it makes far more sense to use querySelectorAll('div.boo .child2, div:not(.boo) .child1') etc and loop through them to add the hide class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.