Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get value of n-th element

Tags:

jquery

Good day. I used search but didn't find the answer I was looking for, I think this is a common question.

Is there any solution to get value of n-th element via jQuery?

Here is simple example: Imagine that we have n inputs with same class. The easiest way to get value of n-th, as I thought $(".some_class").get(n-th).val() the simplest way. But when we use get(), it returns

Object #HTMLInputElement

So after using on it val() we got error:

Uncaught TypeError: Object #HTMLInputElement has no method 'val'.

Is there any solution how use only jQuery methods in this situation?

like image 815
Valery Statichny Avatar asked Mar 22 '23 15:03

Valery Statichny


1 Answers

Use eq instead:

$(".some_class").eq(n-th).val()

eq constructs a new jQuery object from the element you've specified within the set.

like image 120
Alex Avatar answered Apr 05 '23 21:04

Alex