Gardelin
Gardelin

Reputation: 1071

jQuery get all elements beneath specific div

I want to get all script elements that start with same id prefix beneath specific div. Let say i have html code like:

<div id="row-1">
    <script id="fix-1"></script>
</div>
<div id="row-2">
    <script id="fix-2"></script>
</div>
<div id="row-3">
    <script id="fix-3"></script>
</div>
<div id="row-4">
    <script id="fix-4"></script>
</div>

So for example i want to get all script elements beneath div with id #row-2. It should return fix-3 and fix-4.

Upvotes: 1

Views: 309

Answers (2)

Nitesh
Nitesh

Reputation: 1550

Can you please try as:

$("#row-2 ~ div").find("script")

Upvotes: 3

epascarello
epascarello

Reputation: 207527

You want to use nextAll() to get all the siblings after the element.

var elems = $("#row-2").nextAll();
elems.css("background-color", "green");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="row-1">
    <div id="fix-1">X</div>
</div>
<div id="row-2">
    <div id="fix-2">X</div>
</div>
<div id="row-3">
    <div id="fix-3">X</div>
</div>
<div id="row-4">
    <div id="fix-4">X</div>
</div>

So you want the elements combine it with next and find.

$("#row-2").nextAll().find("script")

Upvotes: 5

Related Questions