Solved

Changing table properties with JavaScript

Posted on 2001-09-07
10
565 Views
Last Modified: 2013-11-19
I've made a small table in a html page:

<html><head>
<title>Test</title>
<style type="text/css">
<!--
.tableLayout { background-color: #f1f1f1; border: 1px #999999 solid }
td { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #000000 }
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table cellSpacing=0 cellPadding=2 width=180 border=0 class="tableLayout">
  <tr><td>Menu 1</td></tr>
  <tr><td>Menu 2</td></tr>
  <tr><td>Menu 3</td></tr>
</table>
</body></html>

The table has a backgroundcolor and a small solid border.
Now I would like to create the following effect. When I move the mouse over a cell, the backgroundcolor and bordercolor of that cell has to change. I know how to do it with CSS, but it has to be done width JavaScipt.

I think it should look something like this:
<td onMouseOver="changeLayout();" ...>

Can somebody help how to change html and css elements using JavaScript?

The ideal result I hope to get is something like the left menu on the Microsoft website (http://www.microsoft.com)
0
Comment
Question by:StievieD
  • 8
10 Comments
 
LVL 8

Expert Comment

by:edemcs
ID: 6463995
I'm not sure how to do it with Javascript, and I know you said you know how to do it using css, but if you haven't thought of this method before, here it is.  Good luck


function changerow(objRow,color)
     {
     objRow.style.backgroundColor=color;
     }


<TR BGColor="FFFFFF" OnMouseOver="changerow(this,'eeea88');" OnMouseOut="changerow(this,'FFFFFF');">
0
 
LVL 8

Expert Comment

by:edemcs
ID: 6464003
I'm not sure how to do it with Javascript, and I know you said you know how to do it using css, but if you haven't thought of this method before, here it is.  Good luck


function changerow(objRow,color)
     {
     objRow.style.backgroundColor=color;
     }


<TR BGColor="FFFFFF" OnMouseOver="changerow(this,'eeea88');" OnMouseOut="changerow(this,'FFFFFF');">
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6464008
<!-- These are IE only functions!!  It may be possible to get it to work in
     Netscape 6 if it knows what className is
-->

<html>
<head>
<title> cell changer </title>
<style>
<!--
   .green {background-color:green;color:yellow;border:2px solid navy;
        font-family:arial;cursor:hand}
   .white {background-color:white;color:teal;border:2px solid gray}
   body {background-color:lime}
-->
</style>
<script language="JavaScript">
<!--
   function chgcell(EL,color)
   {
      var lastcell=EL;
      lastcell.className=color;
   }
//-->
</script>
</head>
<body>
<table border=5>
<tr>
<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the first cell
</td>

<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the second cell
</td>
<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the third cell
</td>

<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the fourth cell
</td>
</tr>
<tr>
<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the fifth cell
</td>

<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the sixth cell
    <br>
    <a href="blah.gif" class="specialA"> this is a link </a>
</td>
<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the seventh cell
</td>

<td width=150 height=60 align="center" valign="middle"
    class="white"
    onMouseover="chgcell(this,'green')"
    onMouseout="chgcell(this,'white')">
    this is the eighth cell
</td>
</tr>
<tr>
</table>
</body>
</html>

<!-- using this type of format, we can make any cell
modify itself or any other cell on any supported
event

The current format passes this to the function, but
the function can be easily modified to accept the
id of another cell for modification.

If these are the fucntions you're looking for, we can do what
we have to make them work in your environment. But IE only!
Netscape does not support the className attribute of the DOM

Cd&
-->
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:edemcs
ID: 6464013
I'm not sure how to do it with Javascript, and I know you said you know how to do it using css, but if you haven't thought of this method before, here it is.  Good luck


function changerow(objRow,color)
     {
     objRow.style.backgroundColor=color;
     }


<TR BGColor="FFFFFF" OnMouseOver="changerow(this,'eeea88');" OnMouseOut="changerow(this,'FFFFFF');">
0
 
LVL 8

Expert Comment

by:edemcs
ID: 6464052
I'm not sure how to do it with Javascript, and I know you said you know how to do it using css, but if you haven't thought of this method before, here it is.  Good luck


function changerow(objRow,color)
     {
     objRow.style.backgroundColor=color;
     }


<TR BGColor="FFFFFF" OnMouseOver="changerow(this,'eeea88');" OnMouseOut="changerow(this,'FFFFFF');">
0
 
LVL 8

Expert Comment

by:edemcs
ID: 6464067
first of all, sorry about all of the posts, I must have refreshed the screen to many times.  My method does work on Netscape 6 and IE.
0
 

Author Comment

by:StievieD
ID: 6464102
edemcs Your solution works really great. I still have a single question.

In your solution you use 'objRow.style.backgroundColor' Which other options can I use behind 'style.' What can use for example to change the border size and color also?

I ask this because the words used in JavaScript and CSS are not the same. Example: CSS=background-color <=> JavaScript=backgroundColor.

Have you got a list or something of the thing I can change with 'style.'?
0
 
LVL 8

Accepted Solution

by:
edemcs earned 200 total points
ID: 6464193
0
 
LVL 8

Expert Comment

by:edemcs
ID: 6464239
StevieD, no A??!!! :) Thanks.
0
 
LVL 8

Expert Comment

by:edemcs
ID: 6464243
StevieD, no A??!!! :) Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question