Jump to content


Photo

Changing onmousedown events?


  • Please log in to reply
3 replies to this topic

#1 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 21 October 2006 - 09:23 PM

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:

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');"/>
Now, the bit that doesn't work is the document.getElementById("square").onmousedown = "iAm('circle')"; - Is this possible, if so, how is it done?

Many thanks :)

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 October 2006 - 09:51 PM

have you tried something like this.

functon swapImage(target, image) {
  document.getElementById('square').src = 'circle.gif';
  if (document.images) {
      document[image].onmouseover = "yourfunction('target', 'image');";
  }
}

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 22 October 2006 - 10:25 AM

Thanks for the suggestion, but I found a different way of going about it:
node.attributes["onmousedown"].value = "myFunction('Hello World')";


#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 October 2006 - 09:22 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users