Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning "onmouseenter" programatically

Status
Not open for further replies.

gxpark

Programmer
Aug 10, 2002
19
MX
Hi,

I'm trying to assign the "onmouseenter" event programatically with the following code:

function cscript(root) {
for(var i=0;i<root.childNodes.length;i++) {
btn = root.childNodes(i);
if (btn.className.toLowerCase().length > 6) {
chk = btn.className.toLowerCase.substring(0, 7);
} else { chk = &quot;nope&quot;; }

if (chk == &quot;toolbar&quot; || chk == &quot;menubar&quot;) {
btn.onmouseenter = mhover(btn);
btn.onmouseleave = mleave(btn);
}
cscript(btn);
}
}

You've probably spotted the error already since I'm a beginner...

I call the script at the end of the page as &quot;cscript(document)&quot;.

I have several TD's whose class name is set to &quot;toolbar&quot; and &quot;menubar&quot;.

Somehow, if I add an &quot;alert&quot; to tell me all the &quot;class names&quot; that are &quot;walked&quot;, I never get a &quot;menubar/toolbar&quot;...

Thanks in advance.

Ivan V.
 
Ivan,

you may be able to achieve this like so:
[tt]
btn.onmouseenter = new function(){mhover(btn);}
btn.onmouseleave = new function(){mleave(btn);}
[/tt] =========================================================
if (!succeed) try();
-jeff
 
I don't really know a whole lot about the DOM (hardly anything actually) and using childNodes and stuff like that.

However, the way I understand it, childNode will only refer to the &quot;next level&quot; down so to speak.

For example, the child of document would be things like the <body> tag. The child of body would be things like the table tag. The child of the <table> tag would be things like the <tr> tag. The child of the tr tag would be things like the <td> tag.

So you are looking for the <td> but the td is NOT a child of the document.

The solution? I'm not sure at this moment, but I'll keep looking.
 
I've found a really simple (a lot simpler) and more elegant way. I can't post it because I'm now at home. Let me know if someone is interested to post it tomorrow (it's a great example on how to handle events for a particular kind of tag).

As for HTML being the only child node, well, the function I wrote first is recursive, so that wasn't a problem.

Thank you all.

(That DOM link will come in handy!)

Ivan V.
 
Yeah, I'm always interested in code that is more elegant and more simple.

Let's see it.


isNaN
 
Sure, here it is:

function cscript(root) {
if (document.getElementsByTagName)
var x = document.getElementsByTagName('TD');
else if (document.all)
var x = document.all.tags('TD');
else return;

for (var i=0;i<x.length;i++)
{
if (x.className == &quot;menubar&quot; || x.className == &quot;toolbar&quot;) {
x.onmouseover = mhover;
x.onmouseout = mleave;
x.onmouseup = mclick;
}
}
}

This function first checks the availability of the required functions and then promptly walks only through the &quot;TDs&quot;, which is faster than walking all the HTML.

Quite handy if you ask me. Previously my way of doing this was to individually assign a script to each TD directly into the HTML!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top