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
https://forums.phpfreaks.com/topic/102860-function-event-handler/
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

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.