Table Rollover and Edit Cell using Javascript

Downloads:616 Bookmark this code Bookmark and Share
Rank : 4/5
Rated by : 2 user(s)
Developer:Richard Clark  |  
Date Uploaded:July 27,2009
Level:Intermediate
Size: 0 Bytes
Category:Javascript -> Miscellaneous
Developer Says:

There are times when you want to get tabular data from the user but don't want to use regular HTML controls so, this code shows you how to rollover a cell and edit cell text using javascript. Click Here to View Online Demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Table Rollover and Edit Cell using Javascript</title>
<script language="javascript"> 
	var selectedCell=null;
	function editCell(id){
		if(selectedCell!=null){
			selectedCell.className='normal';
		}
		selectedCell=id;
		selectedCell.className='selected';
		document.getElementById('text').value=id.innerHTML;
		document.getElementById('text').focus();
	}
	function setClass(id,cName){
		if(id.className!='selected'){
			id.className=cName;
		}
	}
	function save(){
		if(selectedCell!=null){
			selectedCell.innerHTML=document.getElementById('text').value;
		}
	}
 </script>
<style>
.hover{
	background-color:#fff;
	cursor:pointer;
}
.normal{
	background-color:#f4f4f4;
}
.selected{
	background-color:#FCF;
}
</style>
</head>

<body style="font-family:Verdana, Geneva, sans-serif;">
Click on a cell to edit text<br />
<table cellpadding="40px" cellspacing="1px" bgcolor="#CCCCCC">	
    <tr>
        <td id="cell_11" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')" onclick="editCell(this)">&nbsp;</td>
        <td id="cell_12"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_13"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_14"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
    </tr>
    <tr>
        <td id="cell_21"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_22"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_23"  class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_24" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
    </tr>
    <tr>
        <td id="cell_31" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_32" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_33" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
        <td id="cell_34" class="normal" onmouseover="setClass(this,'hover')" onmouseout="setClass(this,'normal')"  onclick="editCell(this)">&nbsp;</td>
    </tr>
</table><br />
<textarea rows="3" cols="38" name="text" id="text"></textarea><br />
<input type="button" value="Save" onclick="save()" />
</body>
</html>

The above code was highlighted with Neat Highlighter


User Reviews
Tim
[30-Nov-2009]
#1

This is a very useful code, i needed to create an event calender, helped me a lot. Thanks

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Rate this code
Poor   1 2 3 4 5   Outstanding
Are you human ?

Enter the code shown above