Any documentation for element.nodeIndex?

Showing 1-2 of 2 messages
Any documentation for element.nodeIndex? Justin Dearing 8/27/09 5:49 AM
Hello,

I was editing some javascript code yesterday that happened to use
jquery. Part of the code selected two elements via jquery. Then the
original author used nodeIndex to determine the position of each of
these nodes in their parents. I could not find any documentation on
nodeIndex on the jQuery site or anywhere else on the internet. It
seemed to work in IE8, but I did not see the property from FireBug in
FireFox 3.5. So I removed the call to nodeIndex and used a for loop to
calculate my own equivalent of the nodeIndex.

Is nodeIndex officially supported or meant for internal use? Is it
documented anywhere?

Regards,

Justin Dearing
http://justaprogrammer.net
Re: [jQuery] Any documentation for element.nodeIndex? Karl Swedberg 8/27/09 8:07 AM
it's meant for internal use only. The jQuery core file uses it to calculate nth-child. Here is the source (with my emphasis):

CHILD: function(elem, match){

// ...
case 'nth':
var first = match[2], last = match[3];

if ( first == 1 && last == 0 ) {
return true;
}
var doneName = match[0],
parent = elem.parentNode;
if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
var count = 0;
for ( node = parent.firstChild; node; node = node.nextSibling ) {
if ( node.nodeType === 1 ) {
node.nodeIndex = ++count;
}
parent.sizcache = doneName;
}
var diff = elem.nodeIndex - last;
if ( first == 0 ) {
return diff == 0;
} else {
return ( diff % first == 0 && diff / first >= 0 );
}
}

// ...
},

--Karl