jQuery has quickly become one of the defacto JavaScript libraries for web developers. Its popularity has increased for many reasons including its ease of use, its built-in methods that aid in basic tasks, and its multitude of plug-ins that can be found to enhance its capabilities.
However, when I was writing a function the other day, I came across a minor stumbling block in jQuery. I was trying to create universal âcontinueâ buttons throughout some user-interface tabs and I realized I needed a specific method that isnât part of the jQuery library; some way to determine the current index of the element I was on.
Having been to jQuery Docs several times, I didnât recall ever seeing what I needed, but I was hoping Iâd be lucky. Unfortunately, after a visit to the documentation, I found that I was right. There was no such method. Of course, the eq()
method does exist, but thatâs the direct opposite of what I was looking forâto move to a specific index position of the matched elements. Moreover, in using the eq()
method youâd need to know which index youâd like to select!
The eq()
selection concept is much like the built-in âselectâ parameter that you can pass through the UI Tabs method I was hoping to use. For instance:
$tabs.tabs(âselectâ, 1);
This will select the first tab in the set. So, if I had a continue button at the bottom of each tab panel how would I know how to select the very next tab? I could, of course, make a special case for every instance of a panelâs continue button, but that poses a problem with extensibility. What if we added more tabs? Plus, it’s just ugly code. The fault here is that you’d have to know what each tab is named and how they relate to one anotherâcreating a problem if the tabs were ever re-organized.
Instead Iâd need something a little more universal. I’d need a single function that would encompass all of the continue buttons and switch to the next tab based on its current index. But, as I discovered in the documentation, you canât locate the current index with a simple method like eq()
.
With all of my struggles aside, the good news is that the round-about way of getting the current index position is really not all that difficult. After a few minutes of thought and some idea bouncing (off of the smarter programmers here in the office) we came to the conclusion that I could simply count how many elements there are before the tab Iâm on using the prevAll()
and length
methods built-in to jQuery. Thus, if there were 2 previous elements, I must be at the third item. The selection looks something like:
var $tabs = $(â#tabs-area ul.ui-tabs-navâ).tabs({ fx: { opacity: âtoggleâ } }); $(â#tabs-area a.tab-linkâ).click(function () { currentIndex = $(this).parent().prevAll().length; alert(currentIndex); return false; });
Here Iâm selecting the anchor elements with classes of âtab-linkâ which is what I called each âcontinueâ button. When theyâre clicked, I store the number of âui-tabs-panelsâ before the current tab. Please note, that the parent()
method is being used to traverse backwards to these âui-tabs-panels.â You’re path may vary depending on your HTML markup.
Using âcurrentIndexâ I can check if it is larger than the length of the tabs array (meaning Iâm not on the last tab yet) to prevent any possible tab selection errors by trying to move past the last tab. Then I can use currentIndex to select the next tab. This would make our little function look like this now:
var $tabs = $(â#tabs-area ul.ui-tabs-navâ).tabs({ fx: { opacity: âtoggleâ } }); $(â#tabs-area a.tab-linkâ).click(function () { currentIndex = $(this).parent().prevAll().length; if (currentIndex < $tabs.tabs(âlengthâ)) { $tabs.tabs(âselectâ, currentIndex); } return false; });
I also must mention that in this function Iâm selecting currentIndex in the statement $tabs.tabs(âselectâ, currentIndex);
. This may be confusing since the way the array indices and the âselectâ parameter work are based on a zero beginning index. But, when length is calculated it starts with 1. So, thereâs a bit of an âoff-by-one-bug.â I can, of course, trim the length by 1 and adjust the if statement to be <=
, but that would be equally confusing. Either way, rest assured that the currentIndex in the âselectâ statement will select the very next tab.
In closing, it is quite easy to determine the current index position of an element in jQuery without using any built-in methods. All you need to do is count:
currentIndex = $(this).parent().prevAll().length;
This assignment is very easy to build upon (or adjust the index by subtracting 1âif youâre picky).
Leave a Reply