daviddorsey4 daviddorsey4 - 1 year ago 186
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

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

Here's what I'm trying to accomplish:


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


$("#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 Source

You can use next():

$("#foo").find("h2").html("New header")
         .next("p").html("New Text");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download