Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Event Handlers in script code?

Event Handlers in script code?
Thread Tools
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Mar 20, 2004, 12:40 AM
Okay, I'm not very advanced at JavaScripting, and I don't really like using it too much...

But say I want to make a script that automatically handles the onMouseOver and onMouseOut events, but I want these to be specified in the script itself, not in the link, how would I go about doing that?

I'm not sure I'm making myself clear here... To take an example, say I have a little menu with each menu point being set up as a TD in a table (I know, it's not really tabular information, but it works the best in this case). I then want the background colour of each TD to change on mouse-over. This would of course be very simple to do by simply doing:

<td onmouseover="....." onmouseout=".....">...</td>

But this is so messy and unflexible and annoying...

So I thought I would write a short, simple script that did the same thing - but no luck! I've tried Googling it, but I can't find anything about it, so now I'm asking here...

What I had in mind was something like this:

function switchOn() { this.style.backgroundColor = "#B0D0FF"; } function switchOff() { this.style.backgroundColor = "#FFFFFF"; } document.getElementByTagName('td').onMouseOver = switchOn(); document.getElementByTagName('td').onMouseOut = switchOff();
Obviously, this doesn't work or I wouldn't be asking, but you get the idea...

Any way to do that?
Chris O'Brien
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Mar 20, 2004, 08:43 AM
Standard based browsers and IE handle thing differently, so do something like:
if (document.addEventListener) { document.getElementById("whatever").addEventListener("mouseover", doSomething, false); } else { document.document.getElementById("whatever").attachEvent("onmouseover", doSomething); }
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Mar 20, 2004, 11:12 AM
Thanks - I finally got it to work after finding out that the event had to be all lowercase and to scratch the brackets when calling the function...
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Privacy Policy
All times are GMT -4. The time now is 12:31 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,