How do I select a single element in jQuery?

If you prefer keeping a jQuery object, you may write instead:

$("selector").first().val()

$("selector").eq(5)

This returns a first class jquery array with just the 5th item. i.e.,I can do jquery functions on the return value.

Found the answer here: https://forum.jquery.com/topic/jquery-class-selectors-referencing-individual-elements


jQuery always returns a set of elements. Sometimes, the set is empty. Sometimes, it contains only one element. The beauty of this is that you can write code to work the same way regardless of how many elements are matched:

$("selector").each(function()
{
   this.style.backgroundColor = "red";
});

Fun!


If you find that you know you will only deal with a single element and that only one element will be returned, you can always select the zero index of the array.

$("selector")[0].value 

It's dirty and breaks the convention of jQuery in general... but you "could" do this.