[thelist] JS: retrieve element by tabindex value

Christian Heilmann codepo8 at gmail.com
Wed Jan 18 16:30:35 CST 2006

> Well, this might take a while, but how about looping through all
> elements and reading the tabindex?
> var allElm=document.getElementsByTagName("*");
> for(var i=0;i<allElm.length;i++){
> if(allElm[i].getAttribute('tabindex')==''){continue;}
> if(allElm[i].getAttribute('tabindex')=='401'){var
> elmID=allElm[i].getAttribute('id');break;}
> }
> might be faster with DOM1 reads and a variable:
> var allElm=document.getElementsByTagName("*");
> for(var i=0;i<allElm.length;i++){
> tabidx=allElm[i].tabindex;
> if(!tabidx || !tabidx!='401'){continue;}
> if(tabidx=='401'){var elmID=allElm[i].getAttribute('id');break;}
> }

After quickly checking it on the PC there are two changes:
getAttribute("tabindex") works, but returns a number, so no need for '
 ' in the tests.
the property check allElm[i].tabindex needs to be allElm[i].tabIndex.

Otherwise, it does the trick.

Chris Heilmann
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
Binaries: http://www.onlinetools.org/

More information about the thelist mailing list