views:

83

answers:

2

I have the following HTML and the java script below to simulate the background color change when the link block is clicked, but it doesn't seem to work. Any reason why?

If I have only the onmousedown event handled, the background color will be changed to blue for sure. But if both onmousedown and onmouseup are handled, nothing would change visually.

<div class='Button'><a href='mylink' onmousedown=\"changeColorOnMouseDown();\"  onmouseup=\"changeColorOnMouseUp();\"><span id='note'>note...</span></a></div>


function changeColorOnMouseDown()
{
    document.getElementById('note').style.background='blue';
}

function changeColorOnMouseUp()
{
    document.getElementById('note').style.background='#d8dde7';
}
A: 

Try this:

<div class="Button"><a href="#" onMouseDown="changeColorOnMouseDown();"  onMouseUp="changeColorOnMouseUp();"><span id="note">note...</span></a></div>


function changeColorOnMouseDown()
{
    document.getElementById('note').style.backgroundColor = 'blue';
}

function changeColorOnMouseUp()
{
    document.getElementById('note').style.backgroundColor = '#d8dde7';
}
Sarfraz
hmm, still doesn't work.
tom
@tom: See my updated answer plz.
Sarfraz
yep, that works now! thanks!
tom
@tom: You are welcome :)
Sarfraz
sorry, one more question, it seems that only onmouseup event is handled, but not the onmousedown? such that i don't see the blue color but only see the 2nd color on the screen, is that expected?
tom
when you press the mouse button and hold it over the link it should fire. you can test with an alert in the function.
Sarfraz
A: 

In addition to what Sarfraz advised (style.backgrounColor vs style.background), you don't need to escape the quotes for the function call in your events:

<a href='mylink' onmousedown="changeColorOnMouseDown();"  onmouseup="changeColorOnMouseUp();">

Either user single quotes or double quotes. Again, no need to escape the quote (\").

James H
correct, that's a bad miss. now, what i am seeing is that only onmouseup is handled and the color is changed for that event, but the color is not changed for the onmosuedown event, why is that?
tom