views:

71

answers:

3

Hello, how can I change the background color of current element using plain javascript? for instance

<li onmouseover="this.backgroundColor:#000;">something</li>

This doesn't work, but you will get the idea what I want to do. Thank you

+3  A: 

Almost there.

<li onmouseover="this.style.backgroundColor='#000';">something</li>

http://codepunk.hardwar.org.uk/css2js.htm

Daniel A. White
Is it better to use hover css or javascript for this sort of things? I mean what is more elegant (better practice)
Gandalf StormCrow
Are you just changing it one time? How important is cross browser? Do you have jquery?
Daniel A. White
It's very important that it should be cross-browser .. I used your javascript answer it works in IE8 most probably in IE7 as well .. and firefox of course, it renders almost anything .. I'll accept your answer. I won't use jquery cause I will not use a lot of its cool features, I just need "small" enhancements nothing fancy and no need to load additional nkb cause my background is pretty heavy ..
Gandalf StormCrow
though css must be used indeed to solve the problem, i would not recommend inline javascript too. myLi.onmouseover=function(){} or something else is much better anyway. i don't know which browser excluding ie6 has not :hover support
Lyubomyr Shaydariv
I just tried IE8 , currently using it to view this website .. and it doesn't work and still plenty of people(the ones with illegal copy of windows) use IE6
Gandalf StormCrow
+2  A: 
this.style.backgroundColor= 'black';

Or if you can, better in simple CSS:

li:hover { background-color: black; }
bobince
Not all browsers fully suport the :hover psuedo-class
Daniel A. White
hmm I didn't know that hover works with li, I thougt its only used for a element
Gandalf StormCrow
@Gandoalf, it does it more modern and standards compliant browsers.
Daniel A. White
Hmm.. it doesn't work in IE8 and most probably not in IE7 .. the hover thing
Gandalf StormCrow
the hover pseudoclass does work in IE6+ on elements that already have that functionality. So pretty much just `<a>` tags.
Alex Sexton
first, ie6 is gonna die. second, since ie6 supports a:hover only, we can use "a:hover element" hack to simulate hover behavior with no javascript at all
Lyubomyr Shaydariv
I already have a tag inside <li> but I want the whole block to change the background
Gandalf StormCrow
`li:hover` works on IE7 in Standards Mode (and you should definitely be using a Standards Mode DOCTYPE). If you really need IE6 to look ‘just right’ (personally for minor styling issues I don't always bother any more), then either put the hover on the `<a>` and style that with `display: block` to make it take up the whole width, or if that's not possible the old-school JS is the only solution left.
bobince
also i would like to say, i totally agree with bobince's last comment and two solutions he proposed (sure, the css solution is much better in the case). regarding to the javascript usage, i always divide javascript and markup to make the markup free of scripts and clear as it was shown in the post
Lyubomyr Shaydariv
Indeed. Inline attributes are good for demo code, but in real life binding from script is almost always the better route.
bobince
A: 

CSS hover would probably be the most elegant solution, there is an IE6 fix for this: csshover.htc

But if it were me I would use jQuery, and it would provide you excellent cross-browser support.

jnunn