page = $("<div id="content"><p>aaa</p><p>bbb</p><p>ccc</p></div>").find('p').eq(0); This can echo `<p>aaa</p>` but How to select multi `eq()` in jqeury? if I need `<p>aaa</p><p>bbb</p>`
page = $("<div id="content"><p>aaa</p><p>bbb</p><p>ccc</p></div>").find('p').eq(0); This can echo `<p>aaa</p>` but How to select multi `eq()` in jqeury? if I need `<p>aaa</p><p>bbb</p>`
used .eq() to pull a single paragraph by index, so you only got one element. was right that removing the index returns all and you can hide unwanted items, but if you want to select a specific group of paragraphs as a set there are clearer ways than calling .eq() repeatedly.
For contiguous ranges (first two, first three, etc.) use methods/selectors that return multiple elements:
var $firstTwo = $('#content p').slice(0, 2); var $firstTwo = $('#content p:lt(2)'); var $firstTwo = $('#content p:nth-child(-n+2)'); Notes: .slice(start,end) uses 0‑based start and an exclusive end. :lt(n) is 0‑based too. :nth-child() is CSS 1‑based and matches element position among siblings, so it can behave differently if there are other node types between your p elements.
For non‑contiguous picks (for example, 1st and 3rd), filter by index or build a small jQuery set:
var $sel = $('#content p').filter(function(i){
return i === 0 || i === 2;
}); var $ps = $('#content p');
var $sel = $([$ps.get(0), $ps.get(2)]); // combine DOM nodes into a jQuery object Practical tips: scope selectors to the containing element (avoid selecting all p globally), prefer .slice() for contiguous ranges for clarity and speed, and be careful with :nth-child() when non-p siblings exist. See the jQuery docs for .slice and the :lt selector for details: jQuery .slice and lt selector.
take the eq() out? that way you'll have all three paragraphs.
if you only want the first two to show up, just write.
$('p:last').hide(); if you want the middle one to hide, write
<div id="content"><p>aaa</p><p>bbb</p><p>ccc</p></div>
$('div#content').find('p').eq(1).hide(); you can go here and play with this.
hope this helps
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.