Jump to content

function() event handler


Goose

Recommended Posts

Here is my situation:

 

var myString = 'this is the right string';
obj.onmouseover = function(e) { showTip(e, 'this is the wrong string'); };

 

Since the onmouseover function can't see the myString variable, due to scope, I am unable to pass the contents of myString to the function. I thought I would try a global variable, but since the function that does this will get called multiple times it will only use the last value that the global variable was set to. Is there a way to pass multiple arguments into the onmouseover function?

 

I know this is incorrect, but something like this:

var myString = 'this is the right string';
obj.onmouseover = function(e, myString) { showTip(e, myString); };

 

Thanks much.

Link to comment
Share on other sites

I assume you are using this for a tooltip, since each object (html element) will have it's own tip, you can simple keep the string value inside the function () (like the wrong string) for each object.

 

Second option is to use the title attribute in the HTML element and use the "this" scope in your function to get the title

 

for example

 

HTML...
<input type="text" title="Hello World" id="input_1" />

Script...
document.getElementById('input_1').onmouseover = function(e){
    showTip(e, this.title);
};

Not Tested

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.