CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Collection

  1. #1
    Join Date
    Nov 2000
    Posts
    20

    Collection

    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>

  2. #2
    Join Date
    Feb 2003
    Location
    Oakland, CA
    Posts
    29
    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
    Nate Grover
    http://www.nategrover.com

  3. #3
    Join Date
    Nov 2000
    Posts
    20

    Collection

    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 !

  4. #4
    Join Date
    Feb 2003
    Location
    Oakland, CA
    Posts
    29
    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.
    Nate Grover
    http://www.nategrover.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured