|
Javascript events |
|
monkeyson2 (11:40 25/8/2004) Phlamethrower (19:20 25/8/2004) Matthew (19:57 25/8/2004) monkeyson2 (20:01 25/8/2004) tribbles (21:38 25/8/2004) Matthew (22:04 25/8/2004) monkeyson2 (22:13 25/8/2004)
|
|
Phil Mellor |
Message #57526, posted by monkeyson2 at 11:40, 25/8/2004 |
Please don't let them make me be a monkey butler
Posts: 12380
|
If you've got a function that takes a parameter
function example(foo) { window.alert(foo); }
In a tag you can do something like this
<li id="li1" onmouseover="example('bar')">...</li>
How would you do it within a function?
As far as I can tell you can only do li1.onmouseover = example;
Is there any way to specify the parameter too? |
|
[ Log in to reply ] |
|
Jeffrey Lee |
Message #57540, posted by Phlamethrower at 19:20, 25/8/2004, in reply to message #57526 |
Hot Hot Hot Hot Hot Hot Hot Hot Hot Hot Hot Hot Hot stuff
Posts: 15100
|
As a rough guess, I'd say li1.onmouseover = "example('bar')"; |
|
[ Log in to reply ] |
|
Matthew Somerville |
Message #57543, posted by Matthew at 19:57, 25/8/2004, in reply to message #57526 |
Posts: 520
|
li1.onmouseover = example; I think you mean document.getElementById( "li1" ).onmouseover = example; ?
Is there any way to specify the parameter too? I don't believe so; the only thing passed to the example function will be an event object. You could put the variable you want in an attribute (e.g. class="bar" ), and then use the event object to get at that value. A bit roundabout, any reason you can't put the call in the element? |
|
[ Log in to reply ] |
|
Phil Mellor |
Message #57544, posted by monkeyson2 at 20:01, 25/8/2004, in reply to message #57543 |
Please don't let them make me be a monkey butler
Posts: 12380
|
A bit roundabout, any reason you can't put the call in the element? Because there are about 60 elements that need onmouseover and onmouseout events - which means the html file is too big.
My first plan was to apply the onmouseout event in the function that calls the onmouseover event -- thus halving the space required. |
|
[ Log in to reply ] |
|
Jason Tribbeck |
Message #57552, posted by tribbles at 21:38, 25/8/2004, in reply to message #57544 |
Captain Helix
Posts: 929
|
My first plan was to apply the onmouseout event in the function that calls the onmouseover event -- thus halving the space required. How about keeping an array of IDs to parameters? I think - can you get the ID of the item being clicked on/off? |
|
[ Log in to reply ] |
|
Matthew Somerville |
Message #57554, posted by Matthew at 22:04, 25/8/2004, in reply to message #57552 |
Posts: 520
|
<script type="text/javascript">
function onmouseover(e) { /* Cookie-cutter code to find the source of the event */ if (typeof e == 'undefined') { var e = window.event; } var source; if (typeof e.target != 'undefined') { source = e.target; } else if (typeof e.srcElement != 'undefined') { source = e.srcElement; } else { return; } /* End cookie-cutter code */
/* DO STUFF HERE */
} (Taken from http://www.sitepoint.com/article/simple-tricks-usable-forms.) I think you should be able to do what you want, monkeyson, might involve some lateral thinking. |
|
[ Log in to reply ] |
|
Phil Mellor |
Message #57556, posted by monkeyson2 at 22:13, 25/8/2004, in reply to message #57554 |
Please don't let them make me be a monkey butler
Posts: 12380
|
Ooh, is that an advanced form of 'this' ?
That might be just what I need. I will try it tomorrow |
|
[ Log in to reply ] |
|
|