0

Hello,

I am attempting to use JavaScript to 'highlight' text by changing it's background color:

<div id="foo" onMouseOver="highlight(this);"></div>

In JavaScript:

function highlight (word) {
document.getElementById (word.id).bgColor="blue";
}

This produces no effect. I also have an onClick event in the same div, can I not have onClick and onMouseOver together? Or am I doing something else wrong?

Thanks.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by ItecKid
0

This might not be what you want, but can't you use the :hover pseudo class on a div to set the background color?

0
function highlight(word) {document.getElementById (word).bgColor="blue";}

you will also require another javascript and an onmouseout() to reset the background or in short order everything will be blue
css is smaller faster and works when javascript is disabled for security reasons

.hl {background:#ffffff; }
.hl:hover { background:#0000ff; }
<span> this does not highlight on mouseover</span>
<span class='hl'> this does highlight on mouseover</span>

Edited by almostbob: n/a

0

Thanks for the help guys, much easier than what I was trying to do. :) Marking solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.