Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select first N elements with jquery?

<div class="nav-top">
<ul>
  <li class="tab1"><a href="/">test</a></li>
  <li class="tab2"><a href="#">test</a></li>
  <li class="tab3"><a href="#">test</a></li>
  <li class="navahead"><a href="#">test</a></li>
  <li class="navahead"><a href="#">test</a></li>
<li class="new"><a href="#">test</a></li>
</ul>
</div>

i want to only add onmouse over event to the first three li(tab1,tab2,tab3). how to write the if condition.

like image 465
zhuanzhou Avatar asked Jul 18 '11 08:07

zhuanzhou


People also ask

How do I get the first element of a list in jQuery?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).

What is '$' in jQuery?

$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.

Can you select multiple elements in jQuery?

In jQuery, you can select multiple elements by separate it with a comma “,” symbol.

What is nth child in jQuery?

jQuery :nth-child() Selector The :nth-child() selector in jQuery is used to select all elements that are the nth child, regardless of type, of their parent.


4 Answers

You don't need an if - you can use the jQuery :lt() selector

$('ul li:lt(3)').mouseenter(function(){});

Note that the index is zero-based, so the first three are 0, 1 and 2

http://api.jquery.com/lt-selector/

Update July 2020:

As of jQuery 3.4, the :lt pseudo-class is deprecated. Remove it from your selectors and filter the results later using .slice(). For example, :lt(3) can be replaced with a call to .slice(0,3)

like image 135
Adam Hopkinson Avatar answered Sep 22 '22 18:09

Adam Hopkinson


Actually, you don't need any conditional statement, you can do it with a single selector:

$('ul li:first').mouseenter(function() {
});

If you want to further "filter" your <li> nodes, be more specific, like

$('ul li.navahead:first').mouseenter(function() {
});

update:

To answer your updated question:

$('ul li[class^=tab]').mouseenter(function() {
});

^= will select all classnames which begin with a given string ("tab" in this case)

like image 28
jAndy Avatar answered Sep 21 '22 18:09

jAndy


You don't need "if". Use selectors

$("ul li:first")
like image 2
Hnatt Avatar answered Sep 25 '22 18:09

Hnatt


Use .slice(startIndex, endIndex)

$("div").slice(0, 2).remove();
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
like image 2
Roko C. Buljan Avatar answered Sep 24 '22 18:09

Roko C. Buljan