Sole
Sole

Reputation: 3340

Target first and second occurrence of Div on page using Jquery

Hi I am trying to target the first and second occurrence of a div class on a page, I have the following but this does not work?

  var first = ($("#area").find(".cArea")[0]);
  var second = ($("aArea").find(".cArea")[1]);

Any ideas?

Upvotes: 3

Views: 432

Answers (2)

Nicolas
Nicolas

Reputation: 8660

You can use the :eq(index) selector to get directly the first and second one.

var first = ($("#area").find(".cArea:eq(0)"));
var second = ($("aArea").find(".cArea:eq(1)"));

Upvotes: 3

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use the :lt() selector to achieve this. It selects the elements in a collection whose index is lower than a provided value.

var $firstAndSecond = $('#area .cArea:lt(2)');

Upvotes: 4

Related Questions