JavaScript onMouseOver woes
May 10, 2006 12:21 PM   Subscribe

JavaScript filter: how can I change the onMouseOver for an element by name from within another javascript function?

This seems like it should be easy, but I can't seem to make it happen:

I have a page with a bunch of images with unique names (NAME="star177", NAME="star178") etc. I want them to have an onMouseOver rollover, but for that to turn off once they've been clicked. So, it would look something like Then, the stopMouseOver() function has a line like "document.star120.onmouseover = ';"

It seems like I should be able to treat onMouseOver as a property like that but it hasn't worked (and it's not a case sensitivity issue) and my google fu is failing. I thought of a convoluted work-around, but I'd rather keep it simple...how can I disable an onMouseOver from within a JS function?
posted by TunnelArmr to Computers & Internet (7 answers total)
 
Response by poster: Damn it, I used the character codes but it still treated it as a real IMG tag. It should read [img src=whatever name="star120" onMouseOver="do fancy rollover thing" onClick=stopMouseOver()]
posted by TunnelArmr at 12:24 PM on May 10, 2006


I would do this with id's instead of names.

So your images would look like [img src="whatever" id="star120" onmouseover="fancy roll over thing" onclick="stopmouseover()"]

And in stopmouseover, you would do:
document.getElementById("star120").onmouseover = null;

Or if you're paranoid:
if (document.getElementById) {
var image = document.getElementById("star120");
if (image)
image.onmouseover = null;
}
posted by Sxyzzx at 12:32 PM on May 10, 2006


Wouldn't a simple onClick="this.onMouseOver = null;" parameter do the trick?
posted by mkultra at 12:55 PM on May 10, 2006


if you ever want to change the function to do something else, and you don't quite feel like using Ids (which you shoooould)

image = document.getElementsByName("whatevername")[0]
image.onmouseover = function () { return true; }

is an easy enough way.
posted by soma lkzx at 12:59 PM on May 10, 2006


Am I crazy, or can't you just manipulate the DOM so that once you have moused over an element, that element is replaced by one that does not have onMouseOver capability?
posted by bingo at 1:13 PM on May 10, 2006


AFAIK, mkultra's solution is the most efficient--setting the items' onClick event to remove 'this.onMouseOver' ought to work just fine and is the simplest way to accomplish what you want.
posted by cyrusdogstar at 1:35 PM on May 10, 2006


You could set a variable "starXXdone = 1" when it happens the first time, then check for it in your mouseover function. If it exists, don't do the mouseover. It's messy but quick.
posted by AmbroseChapel at 2:10 PM on May 10, 2006


« Older Can I drive across the USA in 12 days?   |   Please Mister Postman Newer »
This thread is closed to new comments.