daviddorsey4 daviddorsey4 - 3 months ago 31
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?


You can use next():

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