Code not working onmouseover

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wbevan20
    New Member
    • Feb 2012
    • 9

    Code not working onmouseover

    Hi

    I have the following code for a dropdown menu that I am creating and the problem is that whilst the variables are being set correctly the for and if statement aren't functioning correctly.

    Instead of allowing the dropdown menu to be activated onmouseover it flashes on/off briefly when the page is loaded and that's it.

    Here is the code that I am working with:

    [CODE=javascript]function gatherItems(c){
    //alert("Function has been called and the id is: "+c);
    container = document.getEle mentById(c);
    items = container.getEl ementsByTagName ('a');
    subs = container.getEl ementsByTagName ('div');
    alert(items.len gth+" "+subs.leng th);
    //alert(items.id) ;
    for(var i=0;i<items.len gth;i++){
    if(items[i].id){
    items[i].onmouseover = show(items[i].id);
    items[i].onmouseout = delay();
    }
    }
    /*for(var s=0;s<subs.leng th;s++){
    if(subs[s].id){
    subs[s].onmouseover = canceldelay();
    subs[s].onmouseout = delay();
    }
    }*/
    }[/CODE]

    Note: The functions being called onmouseover and onmouseout are working fine without this piece of code.
    Last edited by wbevan20; Feb 21 '12, 04:43 PM. Reason: Formatting
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    please try the following explicit closure construct. i think it should work.

    Code:
    for(var i=0; i< items.length; i++) {
        if(items[i].id){
            items[i].onmouseover = function(idx) {
                return function() { show(items[idx].id) };
            }(i);
    
            items[i].onmouseout = delay();
        }
    }
    assigning handlers in a loop leads to always have i = items.length in the end. closuring the index explicitly should fix the issue.

    Comment

    • wbevan20
      New Member
      • Feb 2012
      • 9

      #3
      That has solved the problem of the dropdown not appearing, however it now won't go away without refreshing the page

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        ?? what does the delay-method do? i suppose it should trigger a close action? while assigning the handler you mustn't execute that, assign it like this:

        Code:
        items[i].onmouseout = delay;
        that way you assign a reference to the delay-function which then is executed on mouseout.
        Last edited by gits; Feb 23 '12, 07:10 AM.

        Comment

        • JackieBolinsky
          New Member
          • Feb 2012
          • 6

          #5
          Hi...
          I hope once you wrap the script in $(document).rea dy(function () { }); this will work correctly. Thanks for the code,i run the code but there is a delay only when i mouseover menu3 and i need in others too,only when i mouseover and mouseout in specific divs the settimeout shall be disabled. If you have control over the code, it is a lot simpler to just build the delay into the event handler from the beginning than wrapping it in on the client side.

          thanks,
          jackie

          [Removed URL]
          Last edited by acoder; Mar 6 '12, 11:15 PM. Reason: Do NOT post any more sneaky links!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            @Jackie:

            huh - can you please explain what that means? how is that post related to the above posts? what is menu3, what are the specific divs and how could one have no control over his/her code etc.?

            regards

            Comment

            • wbevan20
              New Member
              • Feb 2012
              • 9

              #7
              Thanks for all your help, it now works perfectly :)

              Comment

              Working...