1

I have a big red button and I'm trying to use javascript to perform the following: -

  1. OnMouseDown change image so the button looks depressed
  2. OnMouseUp return to the initial image AND reveal a hidden div

I can get the onMouse Down and onMouseUp image changes part to work.

I can also get the hidden div to reveal by using OnClick

The problem is I can't get it all to work together.

How do I do this?

BTW, I'm sure its obvious, but I'm fairly new to javascript, so I appreciate your help

Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66
  • Thanks! You guys are legends - used a bit of each of the answers and it's working a treat now...cant explain how happy I am...thanks SO :)! –  Dec 10 '08 at 12:11

3 Answers3

1

You can use semicolons to separate multiple script statements in an event:

<img src="..." alt="..."
  onmousedown="depressed();"
  onmouseup="undepressed(); revealDiv();" />

Also, I believe most browsers support the onclick event:

<img src="..." alt="..."
  onmousedown="depressed();"
  onmouseup="undepressed();"
  onclick="revealDiv();" />

Since you said you had already figured out each of the 3 parts separately, I just wrote function calls that you can replace with your own code for each step.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
0

Without seeing your code, it's hard to say, but I suspect a missing 'return true;' statement at the end of either the onclick or onmouseup event handlers.

Justin Scott
  • 865
  • 4
  • 10
0

It's never recommended to attach events to elements using the attribute notation directly to an html element's tag.

It is a much better practice to seperate the view (being the rendered html) from the controller (the actions happening)

The best way to attach an event is like such:

<img id="abut" />

<script>
var thebutton = document.getElementById('abut'); //Retrieve the button from the page 
thebutton.onmousedown = depressed; //depressed is a function that you declare earlier on...here, you are just passing a reference to it
thebutton.onmouseup = function () {
    undepressed();
    revealDiv(); //Invoke the two functions when the 'onmouseup' is triggered'
};
</script>
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360