daviddorsey4 daviddorsey4 - 4 months ago 55
jQuery Question

How to use multiple .find() after selector in jquery chaining?

Basically what the title says - I'm wondering if there's a way to use

.find()
on the same jQuery selector multiple times. Or maybe using
.find()
more than once isn't the right way to do this?

Here's what I'm trying to accomplish:

HTML

<div id="foo">
<h2>A Header</h2>
<p>Some text</p>
</div>


JS

$("#foo").find("h2").html("New header");
$("#foo").find("p").html("New text");


Webstorm complains about the duplicated jQuery selector. Is there a different/better way to do this?

Answer

You can use next():

$("#foo").find("h2").html("New header")
         .next("p").html("New Text");
Comments