How to access a container within another container in Jquery?

Asked

Viewed 91 times

0

I’m doing a function to modify these groups of buttons, but I don’t know how to access containers inside containers with Jquery.

<div class="buttons">
  <button aria-label="Right">Right</button>
  <button aria-label="Left">Left</button>
  <button aria-label="Center">Center</button>
  <button aria-label="Expand">Expand</button>
</div>

I can access this container with $(selector), but I want in case delete the button with the aria-label="Expand". I thought intuitively of $(selector)[0].$("[aria-label=\"Expanded\"]").remove(), but there was a syntax error. How do I access a container inside another container in Jquery?

2 answers

2


Try to use as follows:

$('.buttons').find('[aria-label="Expand"]').remove();

We are selecting all children from the div that contains the class buttons with the selector $('.buttons'). After that, we ask to find the element that contains the attribute aria-label="Expand", after finding it just remove with the function remove().

1

It’s very simple, use the css structure

$(document).ready(function(){

$("#resultado").text($('.buttons button[aria-label="Expand"]').attr("aria-label"));
//oque você quer
$('.buttons button[aria-label="Expand"]').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <button aria-label="Right">Right</button>
  <button aria-label="Left">Left</button>
  <button aria-label="Center">Center</button>
  <button aria-label="Expand">Expand</button>
</div>
<span id="resultado"></span>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.