View Full Version : Internet Explorer having issues with onMouseOver

09-10-2004, 06:25 PM
I'm brand new at javascript programming and i'm making a script for a company website that displays a menu.

What i want to happen is when a user puts the mouse curser over menu item a submenu drops down, and when the mouse moves to a different menu item the previous submenu retracts and the other submenu drops down.

The way i'm doing it right now works perfectly in Mozilla FireFox, but does not work at all in Internet Explorer 6:

linkelement = document.createElement("a");

Is there an (easy) alternative way that i can apply an onMouseOver action that works for IE as well as Mozilla?

Here is the full (temporary) code:

<script type="text/javascript">

var idgroups = new Array();
var menulist = document.createElement("ul");
var submenulist;
var menulist;
var menuitem;
var linkelement;
var sublinkelement;
var menuelement = document.getElementById("menu");
var menuitemname;
var submenuitemname;
var menuitemvalue;

var menu = new Array();
var menuvars = new Array();
var submenu = new Array();
var subvars;

var menu = ["One","Two","Three"];
var menuvars = ["assassinate","your","parents"];
var submenu = [new Array("HEH","heh","hEh"),new Array("Rofl","Lmao","LoL"),new Array("I","like","poop")];
var subvars = [new Array("...","...","..."),new Array("...","...","..."),new Array("...","...","...")];

function buildmenu() {
menuelement = document.getElementById("menu");

for (menulen=0; menulen!=menu.length; menulen++) {
// here we create teh menu items
menuitem = document.createElement("li");
linkelement = document.createElement("a");
menuitemname = document.createTextNode(menu[menulen]);

// this is whurr we make teh submenu items
submenulist = document.createElement("ul");
for (submenulen=0; submenulen!=submenu[menulen].length; submenulen++) {
submenuitem = document.createElement("li");
sublinkelement = document.createElement("a");
submenuitemname = document.createTextNode(submenu[menulen][submenulen]);

function expand() {
document.write("I hate you.");

<body onload="buildmenu()">
<div id="menu">


Please just focus at the question on hand, i realize my code isn't very well done. Remember I just started programming javascript recently.

09-10-2004, 09:25 PM
I think you need to do something like this:

linkelement.setAttribute("onMouseOver","function() { expand(); };");

onMouseOver requires a pointer to a function, not a function call. onMouseOver is an event-handler, so it makes a call to the function it's pointing at.

Hope that helps,

09-11-2004, 08:26 PM
No that doesn't work either.

09-12-2004, 02:44 AM
try this:

linkelement.onmouseover = function() { expand(); }

setAttribute doesn't work in IE

09-12-2004, 02:54 AM
element.setAttribute does work in iew, or at least, it does mostly work. However, event handlers are not part of the HTML, really. Event handlers belong in the scripting system. They are parsed together with everything else when the document is parsed, and the corresponding script structures are created. However, when setting an attribute using element.setAttribute you don't reparse it and create the scripting structure, the only thing you do is create an HTML attribute.

If you programmatically want to add events, use the element.onevent syntax. If you don't worry about iew at all, use the DOM2Event mechanisms. Don't use the iew proprietary element.attachEvent mechanism unless there is no other way.

Willy Duitt
09-12-2004, 02:57 AM
try this:

linkelement.onmouseover = function() { expand(); }

setAttribute doesn't work in IE

setAttribute does work with IE but you need to use the correct syntax....
Which is: onmouseover NOT: onMouseOver


09-12-2004, 03:50 AM


09-12-2004, 09:27 PM
try this:

linkelement.onmouseover = function() { expand(); }

setAttribute doesn't work in IE

this was the winner. thanks everyone who helped