Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select all except last

Tags:

jquery

Using jQuery how can you select all elements but the last?

<div class='elem'>1</div>
<div class='elem'>2</div>
<div class='elem'>3</div>

For example I want hide div's 1 and 2, but keep three.

like image 387
ScottyG Avatar asked Apr 25 '16 22:04

ScottyG


People also ask

How to exclude selector in jQuery?

To exclude first and second element from jQuery, use the slice() method.

How to use not() in jQuery?

jQuery :not() SelectorThe :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).

How to select elements using jQuery?

jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All selectors in jQuery start with the dollar sign and parentheses: $().

What is the syntax of jQuery to select and handle elements?

Following is the basic syntax for selecting HTML elements and then performing some action on the selected element(s): $(document). ready(function(){ $(selector).


Video Answer


2 Answers

$("div.elem:not(:last)").hide();

or

$("div.elem").not(":last").hide();
like image 139
ScottyG Avatar answered Nov 13 '22 08:11

ScottyG


You can hide by nth-child of element as defined below.

$("div.elem:not(:nth-child(3))").hide();
like image 28
Iqbal Pasha Avatar answered Nov 13 '22 09:11

Iqbal Pasha