zq29 Posted October 21, 2006 Share Posted October 21, 2006 Not sure if my topic title was confusing enough, but I have an image that runs a function when clicked, in the function that is then run, I want to change what function is run when a different image is clicked. An example might make this a little clearer:[code]function swapImage(target) { document.getElementById("square").src = "circle.gif"; document.getElementById("square").onmousedown = "iAm('circle')";}function iAm(desc) { alert("I am a "+desc);}<img src="square.gif" alt="a square" id="square" onmousedown="iAm('square');"/>[/code]Now, the bit that doesn't work is the [i]document.getElementById("square").onmousedown = "iAm('circle')";[/i] - Is this possible, if so, how is it done?Many thanks :) Link to comment https://forums.phpfreaks.com/topic/24695-changing-onmousedown-events/ Share on other sites More sharing options...
tomfmason Posted October 21, 2006 Share Posted October 21, 2006 have you tried something like this.[code]functon swapImage(target, image) { document.getElementById('square').src = 'circle.gif'; if (document.images) { document[image].onmouseover = "yourfunction('target', 'image');"; }}[/code]I have not tried this but.. If it were a link you could do this document.links[link].yourevent = whatever.If this works please let me know. It will be good for future information.Good Luck,Tom Link to comment https://forums.phpfreaks.com/topic/24695-changing-onmousedown-events/#findComment-112479 Share on other sites More sharing options...
zq29 Posted October 22, 2006 Author Share Posted October 22, 2006 Thanks for the suggestion, but I found a different way of going about it:[code]node.attributes["onmousedown"].value = "myFunction('Hello World')";[/code] Link to comment https://forums.phpfreaks.com/topic/24695-changing-onmousedown-events/#findComment-112622 Share on other sites More sharing options...
fenway Posted October 22, 2006 Share Posted October 22, 2006 You could also probably use an anonymous function to call/return your function, though that might be a slight waste of resources.. but it would like nicer. Link to comment https://forums.phpfreaks.com/topic/24695-changing-onmousedown-events/#findComment-112829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.