Question

So I want to toggle a single element in an array of elements how can I do this?

What I have tried

$(".classname")[1].toggle()
Was it helpful?

Solution

The problem with that is that you are getting the element rather than a jquery object. Try this:

$(".classname").eq(1).toggle()

Also, I assume you are looking for the 2nd element using index 1

OTHER TIPS

Try this :

$('.classname').eq(0).toggle()

When using [], you are losing jquery reference and the object become a dom element. DOM element are used with Javascript. Example :

 $('.classname')[0].id //will work since .id is a DOM attribute
 $('.classname').eq(0).id //will not work since it's a jQuery object

Here the jQuery .eq() information page.

Elements are based on a 0 index, wich mean 0 = first element, 1 = second element and go on

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top