Goose Posted April 20, 2008 Share Posted April 20, 2008 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 https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/ Share on other sites More sharing options...
Psycho Posted April 20, 2008 Share Posted April 20, 2008 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 https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/#findComment-522018 Share on other sites More sharing options...
Goose Posted April 20, 2008 Author Share Posted April 20, 2008 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 https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/#findComment-522175 Share on other sites More sharing options...
nogray Posted April 22, 2008 Share Posted April 22, 2008 obj.onmouseover = function(){showTooltip('Some random text.');}; Link to comment https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/#findComment-524139 Share on other sites More sharing options...
Goose Posted April 25, 2008 Author Share Posted April 25, 2008 obj.onmouseover = function(){showTooltip('Some random text.');}; Is there a way to also pass in the event handler with this method? Link to comment https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/#findComment-526850 Share on other sites More sharing options...
Goose Posted April 25, 2008 Author Share Posted April 25, 2008 O figured it out obj.onmouseover = function(e){showTip(e, 'Some random text.')} This allows me to pass the event and another argument into the showTip function. Link to comment https://forums.phpfreaks.com/topic/101947-solved-programatically-setting-a-javascript-event-with-an-argument/#findComment-526872 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.