jQuery find() example
In jQuery, you can use the find() to search through all the descendants(child, grandchild, great-grandchild…any levels deep) of the matched element.
For example, div elements with three levels deep.
<div class="A1"> <div class="child">A1-1</div> <div class="child">A1-2</div> <div class="A2"> <div class="child">A2-1</div> <div class="child">A2-2</div> <div class="A3"> <div class="child">A3-1</div> <div class="child">A3-2</div> </div> </div> </div>
1. $(‘div .A1′).find(‘.child’)
$('div .A1').find('.child').css('background','red');Match div elements which have child contains a class name of “A1“, and search through the “A1” child that contains class name of “child”. In this case, no element is selected, because “div .A1” is not match, there have no child contains class name of “A1“, the “A1” is a parent.
P.S If you want to select all the A1, A2 and A3 child, your code should change to following
$('.A1').find('.child').css('background','red');2. $(‘div .A2′).find(‘.child’)
$('div .A2').find('.child').css('background','red');Match div elements which have child contains a class name of “A2“, and search through the “A2” child that contains a class name of “child”. In this case, all the “A2” and “A3” child are selected.
3. $(‘div .A3′).find(‘.child’)
$('div .A3').find('.child').css('background','red');Match div elements which have child contains a class name of “A3“, and search through the “A3” child that contains a class name of “child”. In this case, only the “A3” child are selected.
jQuery find() example
Play this example.
<html> <head> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <style type="text/css"> div{ padding:8px; border:1px solid; } </style> </head> <body> <h1>jQuery find() example</h1> <script type="text/javascript"> $(document).ready(function(){ $("#button1").click(function () { $('div').css('background','white'); $('div .A1').find('.child').css('background','red'); }); $("#button2").click(function () { $('div').css('background','white'); $('div .A2').find('.child').css('background','red'); }); $("#button3").click(function () { $('div').css('background','white'); $('div .A3').find('.child').css('background','red'); }); }); </script> </head><body> <div class="A1"> <div class="child">A1-1</div> <div class="child">A1-2</div> <div class="A2"> <div class="child">A2-1</div> <div class="child">A2-2</div> <div class="A3"> <div class="child">A3-1</div> <div class="child">A3-2</div> </div> </div> </div> <br/> <br/> <br/> <input type='button' value='find div .A1' id='button1'> <input type='button' value='find div .A2' id='button2'> <input type='button' value='find div .A3' id='button3'> </body> </html>
what a terrible explanation of find() …. lol
This is why people hate javascript, because of poor documentation
May i know how to improve it?
[...] http://www.mkyong.com/jquery/jquery-find-example/ [...]
Can anyone help with a problem with a JQuery Find?
In the example below I want to find the select & text objects within Div1. Here is the HTML:
[code]
:
[/code]
Here is the jQuery:
Select1 = jQuery(“#div1″).find(“#Select1″)[0];
Text1 = jQuery(“#div1″).find(“#Text1″)[0];
Select1 comes back as undefined, but it finds the Text1 object.
Can anyone tell me why I can’t find the Select1 object?
how to do it with ol and li?
<ol id=bbb
<li class=aaa
<li class=aaa
i had tried
$('ol').css('background-color','white');
$('#bbb .aaa').find("*").css('background-color','');
but with no luck…
thanks,
[...] find() selector Select certain elements from a set of the matched elements (Descendants only). [...]