/    Sign up×
Community /Pin to ProfileBookmark

change background colour

How can you change the background colour of a cell when you mouseover it? Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@requestcodeMar 14.2003 — Here is an example that changes the color and text in a table cell.

<html>

<head>

<title>Table Cell Color Change</title>

<script language="JavaScript">

function chgtabcolor(name,bcolor,txt)

{

if(document.getElementById)

{


elm=document.getElementById(name.id)

elm.style.backgroundColor=bcolor

var newText = document.createTextNode(txt)

elm.replaceChild(newText, elm.childNodes[0])

}

}

</script>

</head>

<body>

<table align="center" border="1" width="200" height="120" cellspacing="0" cellpadding="0">

<tr>

<td id="cell1" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'lightgreen','World')" onmouseout="chgtabcolor(this,'white','Hello')"> Hello </td>

<td id="cell2" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'red','World')" onmouseout="chgtabcolor(this,'white','Hello')"> Hello </td>

<td id="cell3" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'lightyellow','World')" onmouseout="chgtabcolor(this,'white','Hello')">Hello</td>

</tr>

<tr>

<td id="cell4" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'lightgreen','World')" onmouseout="chgtabcolor(this,'white','Hello')"> Hello </td>

<td id="cell5" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'red','World')" onmouseout="chgtabcolor(this,'white','Hello')"> Hello </td>

<td id="cell6" align="center" valign="middle" width="66" height="20" onmouseover="chgtabcolor(this,'lightyellow','World')" onmouseout="chgtabcolor(this,'white','Hello')">Hello</td>

</tr>


</table>

</body>

</html>
Copy linkTweet thisAlerts:
@WebskaterauthorMar 14.2003 — Thanks for your reply. Will it work in Netscape? Also, what modifications would I need to make to get it to change the image used as the background for a cell? Again, thanks.
×

Success!

Help @Webskater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...