JavaScript Find elements by class beginning with parent element find_element_by_class,find class elements jquery,find_elements_by_class_name,find elements by class

With a setup like;

var o = $('
    <div class="a a-1">
        <div class="b"></div>
        <div class="c">         
            <div class="a a-2"></div>
        </div>
        <div class="a a-3"></div>
    </div>
');

How do I find all class "a" elements?

When I try o.find('.a'), elements .a-2 and .a-3 are returned without the parent class (.a-1). So I also tried adding a wrapper '<div><div class="a a-1">...</div></div>' but since am using ajax, it messes up front-end rendering.

Any ideas?

Addendum: Someone suggested similarity to jQuery.find() ignores root node. No problem except the question requires capturing one element. In that case, .filter() should suffice. The question asks "How can I grab one..." In this case I need all of them.

Answer:1

For jQuery 1.8 and up, you can use .addBack()

o.find(".a").addBack(".a")

Prior to jQuery 1.8, use .andSelf() (now deprecated) :

o.find(".a").andSelf(".a")
Answer:2

find looks inside the set of elements, so you could do:

var o = $('
<div class="a a-1">
    <div class="b"></div>
    <div class="c">         
        <div class="a a-2"></div>
    </div>
    <div class="a a-3"></div>
</div>
');

Then:

o = o.appendTo($('<div></div>'));

and then:

o.find('.a')...
Answer:3

use .andSelf() << jq 1.8 or .addBack() >>for jq 1.8 it includes also the current targeted node :

var o = $('<div class="a a-1"> <div class="b"></div><div class="c"><div class="a a-2"></div></div> <div class="a a-3"></div></div>');

console.log("number of element with andSelf .a :"+ o.find(".a").andSelf().length);
console.log("number of element with addBack .a :"+ o.find(".a").addBack().length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Answer:4

I need to do let the last reply scroll into view using jQuery. Here is my code: function showHideReplies(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ ...

I need to do let the last reply scroll into view using jQuery. Here is my code: function showHideReplies(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ ...

  1. scrollintoview in jquery

I am using elevate zoom plugin on online shopping website. It is working. but when my product is changing using ajax, it does not set data-zoom-image attribute. Is there any solution to fix this ...

I am using elevate zoom plugin on online shopping website. It is working. but when my product is changing using ajax, it does not set data-zoom-image attribute. Is there any solution to fix this ...

  1. elevate zoom plugin
  2. elevate zoom plugin wordpress
  3. elevate zoom plugin jquery
  4. elevate zoom images plugin

I need to keep the values of each input in an js object. This is my html code : <div class="produs_varianta"> <input type="text" class="product_name" value="Product One"> <...

I need to keep the values of each input in an js object. This is my html code : <div class="produs_varianta"> <input type="text" class="product_name" value="Product One"> <...

I've looked through other posts, and can't quite find the answer I'm looking for. I'm a REALLY new PHP programmer... I currently have the following code: $error_message .= 'The Email Address you ...

I've looked through other posts, and can't quite find the answer I'm looking for. I'm a REALLY new PHP programmer... I currently have the following code: $error_message .= 'The Email Address you ...