Click to See Complete Forum and Search --> : Collection


atreis
March 17th, 2003, 08:30 AM
Hello, friends.

Iīm in trouble with a simple code, but i donīt know how to solve this question.

I have a home page, with one tag with the same id and I need to know the index of an id when the mouse passes over the tag..

The property sourceIndex does not work on this case, as i just have tested.

How can I get the index of some specific id in that collection ????

See below my sample html code:

<HTML>

<HEAD>

<SCRIPT for=linkMenu event=onmouseover>

if window.LinkMenu.sourceIndex = 1 {
window.alert('1');
{
else {
window.alert('2');
}

</SCRIPT>

<BODY>

<A id=LinkMenu href="main.htm">Test 1</A>
<A id=LinkMenu href="old_main.htm">Test 2</A>

</BODY>

</HEAD>

</HTML>

nategrover
March 17th, 2003, 10:41 AM
Just pass the object itself in the mouseover with the "this" keyword.

You can change your mouseover to pass "this" which will be a reference to the object. For instance:

<A HREF="blahblah" onMouseover = "doSomething(this)")>asdf</A>



<SCRIPT>
function doSomething(linkObject){
if(linkObject.tagName == "A"){
alert(this.innerText);
}
}
</SCRIPT>

Hope this helps

atreis
March 17th, 2003, 11:01 AM
Thanks, friend !

In fact, I need to know the index of the element in the collection that was built by browser.

The browser created an collection using my code.


Thanks !

nategrover
March 17th, 2003, 12:21 PM
Ok, if you need to know the index, and the links are being generated by your code, you can just put the index into the id.

BTW, an ID is supposed to be unique. Names can be duplicated but only one instance of an ID can be identified by the DOM.

So, your links should look like this:

<A ID="linkMenu_1" HREF="asdf">test1</A>
<A ID="linkMenu_2" HREF="asdf">test2</A>
<A ID="linkMenu_3" HREF="asdf">test3</A>

Now when your mouseover sends "doSomething(this)", you can parse the index out of the id.

If for some reason that doesn't work for you , you can always start traversing the DOM based on your link object. Like this:

function doSomething(link){
prevLink = link.previousSibling;
index = 0;
while(prevLink != null){
if(prevLink.tagName == "A") && prevLink.className == "menuLinkClass") index++;
prevLink = prevLink.previousSibling;
}

currentLinkIndex = index;

}


Hope this helps.