user1743638
user1743638

Reputation: 11

select classes with jQuery

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p class="myChild">one</p>
<p class="myChild">two</p>
<p class="myChild">three</p>

<script>
$(document).ready(function(){
//code that selects the second and the third myChild class name

});
</script>
</body>
</html>

here i have 3 p tags with myChild class, i want to select the second and the last one using jQuery to do that, and know how to manipulate with that

Upvotes: 1

Views: 94

Answers (4)

Gary Stevens
Gary Stevens

Reputation: 693

$(".myChild") will return a HTMLCollection (sort of like an array) of objects - you can iterate though this or directly reference positions in that collection. e.g. $(".myChild").eq(1) for the 2nd items and $(".myChild").eq(2) for the 3rd item.

Upvotes: 0

GillesC
GillesC

Reputation: 10874

just use the not filter and pass it eq(0) which is basically stripping out the first result

$('.myChild').not(':eq(0)');

or for the shortest way and using only the selector engine

$('.myChild:not(:eq(0))')

Fiddle here http://jsfiddle.net/D5Ngh/

Upvotes: 1

Ranganadh Paramkusam
Ranganadh Paramkusam

Reputation: 4368

If you are checking for the remaining except first one, you can use :not(0)

Demo

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337713

Use slice() to select a subset of results:

$(".myChild").slice(1);

This will remove the first myChild element and return the rest.

Example fiddle


If you want to specify a start and end point to slice at then you supply two parameters:

$(".myChild").slice(1,3);

Another fiddle

Upvotes: 1

Related Questions