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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.