javascript - jQuery find all elements with class beneath parent, even in children elements -
here 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 jquery function:
$(function(){ $(".parent>.searchel").each(function(){ $(this).html("found one"); }); });
the dom elements end so:
<div class="parent"> <div class="searchel">found one</div> <div class="searchel">found one</div> <div class="child"> <div class="searchel"></div> <div class="searchel"></div> </div> </div>
using jquery/javascript, how can search , find elements class .searchel
beneath element .parent
, if within child element, without searching document globally $(".searchel")
?
use space instead of >
$(function() { $(".parent .searchel").each(function() { $(this).html("found one"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="parent"> <div class="searchel"></div> <div class="searchel"></div> <div class="child"> <div class="searchel"></div> <div class="searchel"></div> </div> </div>
Comments
Post a Comment