jQuery .index()? No thanks… .eq() it is!

This is just some new jQuery information that just may save you many pots of coffee.

Problem:
Say you have an array of elements, more specifically, and unordered list (ul, li). You would like to make a “next & previous” type function… you need to make this as “pretty as possible.” You can only do this with nifty CSS Classes but it has to be dynamic. For example, at any one time your list can contain 10 elements or even 2! Well, finally after many /faceplants I’ve figured it out!

Solution:
Here is where I save you time & frustration! If you wanted to, you can google away and it will tell you to use the $(<element>).index(<integer>).doWhateverHere(); feature in jQuery.

Well, don’t! For cryin’ out loud, DON’T! It will just cause you to /faceplant and waste many hours of google’n!

The real solution is to use $(<element>).eq(<integer>).<yourWantedActions>

I hope this helps somebody! I may have been wrong in the sense to use .index(), but that is what the basis of programming has always been! Use the “index” to find a certain element or in this case now-a-days using a declared variable for the object

One Response to “jQuery .index()? No thanks… .eq() it is!”

  1. Timetraveller Says:

    This definitely helped me.

    I had problems with index() function in IE.

    Thanks a million for sharing.

Leave a Reply