Change Background Color

S

shapper

Hello,

How can I change the background color of a DIV when the mouse is over
it?

Thank You,
Miguel
 
B

Benjamin

here's the script:
<script type="text/javascript">
function changeTo(div) {
div.style.backgroundColor = "red";
}
function changeFrom(div) {
div.style.backgroundColor = "white";
}
</script>

Your div should like somthing like this:
<div onmouseover="changeTo(this)" onmouseout="changeFrom(this)">Some
stuff</div>
 
A

ASM

Benjamin a écrit :
here's the script:

and here's another one :

css :
=====
..onRed { background: lightyellow; }
..onRed:hover { background: yellow; }

javascript :
============
var IE = false; /*@cc_on IE=true; @*/

function roll(what) {
if(IE) {
what = what.style;
what.backgroundColor = what.backgroundColor==''? 'yellow' : '';
}
}

html :
======
<div class="onRed"
onmouseover="roll(this);"
onmouseout="roll(this);">
bla bla blabla
</div>
 
R

RobG

ASM wrote:
[...]
css :
=====
.onRed { background: lightyellow; }
.onRed:hover { background: yellow; }

javascript :
============
var IE = false; /*@cc_on IE=true; @*/

IE 7 is supposed to support hover on elements other than A, so you
might want to reconsider using browser sniffing with script and switch
to pure CSS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top