How do I select the div that contains "bar"?
<html>
<body>
<div id="foo">
<div>bar</div> <!--this one-->
<div>abc</div>
<div>123</div>
</div>
</body>
</html>
How do I select the div that contains "bar"?
<html>
<body>
<div id="foo">
<div>bar</div> <!--this one-->
<div>abc</div>
<div>123</div>
</div>
</body>
</html>
Share
Improve this question
edited Sep 7, 2018 at 12:37
Salman Arshad
272k84 gold badges443 silver badges534 bronze badges
asked Dec 9, 2011 at 20:07
eastboundreastboundr
1,8778 gold badges28 silver badges46 bronze badges
1
- Yep, thanks for the editing Dan. – eastboundr Commented Dec 9, 2011 at 20:29
3 Answers
Reset to default 4This is fastest as it does not use pseudo-selectors
$('#foo > div').first()
// same as
$('#foo > div').eq(0)
// same as
$('#foo > div').slice(0,1)
This should do it:
$("#foo :first-child");
If you want based on contents rather than position, you could use this:
http://api.jquery./contains-selector/
$("div:contains('bar')")