Jump to content

[SOLVED] Programatically setting a javascript event with an argument


Goose

Recommended Posts

Basically I have some code in Javascript that looks like this:

 

...
obj.onmouseover = showTooltip;
obj.onmouseout = hideTooltip;
...

 

When I am doing this, is there a way to pass in an argument to the showTooltip function? I know that when the showTooltip function actually gets called it has the object that called it, which can be accessed by the reference this. I know this is illegal and won't run, but I want to do something like this:

 

...
obj.onmouseover = showTooltip('Some random text.');
obj.onmouseout = hideTooltip;
...

 

Thanks so much!

I don't know that you can do that. But, I can think of two solutions that are similar (except where you set the "random" text). Both solutions involve creating a custom attribute for the object and setting the value of that attribute to the "random text". Then in the function showTooltip you don't need to pass it the text - you can access the appropriate text with the attribute, such as "this.getAttribute('tooltip')".

 

Option 1 - Set the attribute when creating the event handlers

...
obj.onmouseover = showTooltip('Some random text.');
obj.onmouseout = hideTooltip;
obj.tooltip='Some random text.';
...

 

Option2 - set the attribute in the field itself

<div tooltip="Some random text">This is the object</div>

Interesting... I didn't know that I could make my own attributes. I think I will explore that option. I know that the second option won't really work because I am dynamically creating the object. I will let you all know how it works out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.