473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. function gatherItems(c){
  2.   //alert("Function has been called and the id is: "+c);
  3.   container = document.getElementById(c);
  4.   items = container.getElementsByTagName('a');
  5.   subs = container.getElementsByTagName('div');
  6.   alert(items.length+" "+subs.length);
  7.   //alert(items.id);
  8.   for(var i=0;i<items.length;i++){
  9.     if(items[i].id){
  10.       items[i].onmouseover = show(items[i].id);
  11.       items[i].onmouseout = delay();
  12.     }
  13.   }
  14.   /*for(var s=0;s<subs.length;s++){
  15.     if(subs[s].id){
  16.       subs[s].onmouseover = canceldelay();
  17.       subs[s].onmouseout = delay();
  18.     }
  19.   }*/
  20. }
Note: The functions being called onmouseover and onmouseout are working fine without this piece of code.
Feb 21 '12 #1

✓ answered by gits

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

Expand|Select|Wrap|Line Numbers
  1. for(var i=0; i< items.length; i++) {
  2.     if(items[i].id){
  3.         items[i].onmouseover = function(idx) {
  4.             return function() { show(items[idx].id) };
  5.         }(i);
  6.  
  7.         items[i].onmouseout = delay();
  8.     }
  9. }
  10.  
assigning handlers in a loop leads to always have i = items.length in the end. closuring the index explicitly should fix the issue.

6 1784
gits
5,390 Expert Mod 4TB
please try the following explicit closure construct. i think it should work.

Expand|Select|Wrap|Line Numbers
  1. for(var i=0; i< items.length; i++) {
  2.     if(items[i].id){
  3.         items[i].onmouseover = function(idx) {
  4.             return function() { show(items[idx].id) };
  5.         }(i);
  6.  
  7.         items[i].onmouseout = delay();
  8.     }
  9. }
  10.  
assigning handlers in a loop leads to always have i = items.length in the end. closuring the index explicitly should fix the issue.
Feb 22 '12 #2
That has solved the problem of the dropdown not appearing, however it now won't go away without refreshing the page
Feb 22 '12 #3
gits
5,390 Expert Mod 4TB
?? 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:

Expand|Select|Wrap|Line Numbers
  1. items[i].onmouseout = delay;
that way you assign a reference to the delay-function which then is executed on mouseout.
Feb 23 '12 #4
Hi...
I hope once you wrap the script in $(document).ready(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]
Feb 23 '12 #5
gits
5,390 Expert Mod 4TB
@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
Feb 23 '12 #6
Thanks for all your help, it now works perfectly :)
Mar 6 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: christian9997 | last post by:
Hi I would be very helpful if someone could help me with this code. It works fine in IE but when I display it in Netscape or Firefox and I move the mouse from one menu to the other the gap...
7
by: Swincher | last post by:
Hey - - Is there any way to do this? Or, lacking that, any way to simulate it? Thanks - -- - Ken
14
by: menakasrinivasagam | last post by:
my coding is working in mozilla firefox.but not working in Internet explorer.plz give suggestion to rectify this problem
5
by: athirai | last post by:
Hi all, I want to scroll within a window. <a href="javascript:window.location('#first');">First</a> When i click First then it moves to the particular anchor tag within the same page. <a...
2
by: Steve Richter | last post by:
Why would this embedded javascript not work? <span onmouseover="this.style.font-weight='bold';" >line 3</span> this does: <span onmouseover="this.style.color='blue';" >line 1</span> thanks,
10
by: Mike | last post by:
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an...
26
by: Jenitha S | last post by:
Hi, I am working in JSP code.A Menubar is displayed in JSP page.It's working fine in IE. But in case of firefox the very first menuitem is not working.If i move that menu item as second means it's...
1
by: suneel271 | last post by:
code working in internet explorer but not in mozilla and iam getting erroe like window.event has no properties and here is the code <%@ page language="java" contentType="text/html;...
3
by: kepskp | last post by:
i just write code for select option in combo box which i m select but when i press search button and then click on back button then there in no option selected. and that same code working in ie7 and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.