tags:

views:

143

answers:

3

I have an image:

<img id="reqPic" src="mark.png" />

I also have declared a flag earlier on in the page:

<script type="text/javascript">
 var isToolTipEnabled = true;
</script>

I now want to be able to check the flag and if its true, assign the onmouseover and onmouseout events. However, the onmousover event has to be changed to another function called Tip('string') which takes in a string. I have seen other questions on here on how to change this but it I dont see how I can pass paramters to the new function I want to change to.

The onClick would be changed to something like this:

onClick="javascript:toggleHelp('reftypeHelp');";

Any help on this would be greatly appreciated.

Thanks!

+3  A: 

document.getElementById("reqPic").onmouseover = function() { Tip('This is the string.'); };

Eric Mickelsen
Does onclick work the same way?
ConfusedJavaScripter
Yes, all dom events should be assignable this way.
Eric Mickelsen
Also, via closure, you can use a parameter to set the string:function setTip(tipString){document.getElementById("reqPic").onclick = function() { Tip(tipString); };}This is one of my very favorite Javascript features.
Eric Mickelsen
A: 

You could have an onload event on your body tag which calls a function that checks necessary values.

<script type="text/javascript">
var isToolTipEnabled = true;
function eventAdder() {
    if (isToolTipEnabled) {
        var img = document.getElementById('reqPic');
        img.onmouseover = function() {
            Tip('string');
        }
        img.onmouseout = whateverElse;
    }
}
</script>
<body onload="eventAdder();">
Sarah Vessels
A: 

with jQuery you could use the hover function but yeah creating an inline function works too

RHicke