Jump to content

Archived

This topic is now archived and is closed to further replies.

Kris

Changing onmousedown events?

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks for the suggestion, but I found a different way of going about it:
[code]node.attributes["onmousedown"].value = "myFunction('Hello World')";[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.