Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery using .toggle() to toggle a single element

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()
like image 343
csteifel Avatar asked Jul 12 '26 08:07

csteifel


2 Answers

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

like image 126
Dallas Avatar answered Jul 14 '26 21:07

Dallas


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

like image 20
Karl-André Gagnon Avatar answered Jul 14 '26 22:07

Karl-André Gagnon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!