jQuery find all elements with class beneath parent even in children elements

0 votes

Here is some sample HTML:

<div class="parent">
    <div class="searchEl"></div>
    <div class="searchEl"></div>
    <div class="child">
        <div class="searchEl"></div>
        <div class="searchEl"></div>
    </div>
</div>

And here is a jQuery function:

$(function(){
    $(".parent>.searchEl").each(function(){
        $(this).html("Found this one");
    });
});

The DOM elements will end up like so:

<div class="parent">
    <div class="searchEl">Found this one</div>
    <div class="searchEl">Found this one</div>
    <div class="child">
        <div class="searchEl"></div>
        <div class="searchEl"></div>
    </div>
</div>

Using jQuery/Javascript, how can I search for and find all the elements with class .searchEl beneath the element .parent, even if they are within another child element, without searching the document globally with $(".searchEl")?

Jun 7 in JQuery by Edureka
• 13,640 points
9 views

1 answer to this question.

0 votes

Remove > from your select

$(".parent .searchEl").

You can use the .find() method also,

$(".parent").find(".searchEl")
answered Jun 7 by Edureka
• 13,640 points

Related Questions In JQuery

0 votes
1 answer

How to find a parent with a known class in jQuery?

Hello @kartik, Assuming that this is .d, you can write $(this).closest('.a'); The closest method returns the ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,720 points
1,850 views
0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello, Here is the syntax using jQuery $.get $.get(url, data, ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
23,811 views
0 votes
1 answer

How can I select an element with multiple classes in jQuery?

Hello @kartik, If you want to match only ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,720 points
1,221 views
0 votes
1 answer

How do I iterate through children elements of a div using jQuery?

Hello @kartik, Use children() and each(), you can optionally pass a ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,720 points
2,604 views
0 votes
0 answers

jQuery find and replace string

I have somewhere on website a specific ...READ MORE

Jun 10 in JQuery by gaurav
• 12,980 points
12 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,510 points
293 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
8,283 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
15,143 views
0 votes
1 answer

addClass and removeClass in jQuery - not removing class

What happens is that your close button ...READ MORE

answered Jun 2 in JQuery by Edureka
• 13,640 points
26 views
0 votes
1 answer

Get class list for element with jQuery

Use the jQuery attr() Method You can simply use the attr() method ...READ MORE

answered Jun 1 in JQuery by Edureka
• 13,640 points
9 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP