How to know which is the first and last element with Jquery?

Asked

Viewed 542 times

0

Good evening, everyone. I have a question. I am doing an image gallery and would like to know how I know what is the first and last element. Like I have the right and left arrows for previous image or next image. Dai type when arriving at the last image I want to hide the arrow to the next image and when I have in the first image I would like to hide the arrow to the previous image. You can help me?

  • You can include the relevant code of your gallery? Usually to do this you store the number of the current item in a variable that is incremented when you advance, and decremented when backwards. Knowing how many items you have in the gallery makes it easy to know if you are on the first or last.

  • @bfavaretto actually I already do that you talked about. I actually just wanted to know what is the first and last element

  • Well, if you already have this variable, just check if its value is zero (or 1, depending on how you implemented it) to hide the left arrow, and compare it to the collection size to hide the right arrow.

1 answer

1


You can use the methods last and first

$(".elemento").first(); // primeiro elemento
$(".elemento").last(); // último elemento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.