Adding addEventListener to a clicked table cell to change the color
I am creating a dynamic table of three columns and multiple rows. I want to be able to click the last cells in each row and have the row be selected showing a certain color. I am trying to do this as well as make sure that if another cell is selected already it will deselect. I am having a few issues not sure exactly what to do. I can create an onclick alert message that works, however no success with the bg color. Any suggestions are helpful. Function createCell should be where this is addressed.
<html>
<br/><br/></p>
<table id="my_table" align="center" cellspacing="0" cellpadding="0" border="0">
<tr>
<td>Name</td>
<td>Age</td>
<td>Sex</td>
</tr>
</table>
<p></center></p>
<script type="text/javascript" language="javascript">
function appendRow(){
var names = ["Paul", "Mike", "Linda"];
var ages = ["16", "23", "44"];
var male_female = ["M", "M", "F"];
var tbl = document.getElementById('my_table'); // table reference
// append table row
var id;
var z=1;
for(k=0;k<names.length;k++){
var row = tbl.insertRow(tbl.rows.length);
var j = tbl.rows.length - 2;
for (var i=0;i<tbl.rows[0].cells.length;i++) {
id=z++;
var cell_text = '';
if (i == 0) {
cell_text = names[j];
} else if (i == 1) {
cell_text = ages[j];
} else if (i == 2) {
cell_text = male_female[j];
}
createCell(id, row.insertCell(i), cell_text, 'row');
}
}
}
function createCell(id, cell, text, style){
var div = document.createElement('div'); // create DIV element
var txt = document.createTextNode(text); // create text node
if(id % 3 == 0)
{
cell.setAttribute('onclick', 'alert("hello")'); //for testing purposes
开发者_如何转开发 cell.addEventListener("click", clickCell, false);
}
div.appendChild(txt); // append text node to the DIV
div.setAttribute('class', style); // set DIV class attribute
div.setAttribute('className', style); // set DIV class attribute for IE (?!)
cell.appendChild(div); // append DIV to the table cell
}
function clickCell()
{
if(e)
e.setAttribute("bgColor","purple");
if(e != this){
e = this;
e.setAttribute("bgColor","blue");
}else{
e = null;
}
}
</script>
<BODY onload="appendRow()">
<style>
table#my_table{border-collapse:collapse;}
table#my_table td{width:50px;height:27px;border:1px solid #D3D3D3;font-size:10pt;text-align:center;padding:0;}
.append_row{background-color:#FFD6D6;border:1px #ccc solid;}
</style>
</body>
</html>
Modify the clickCell
method to:
function clickCell(e) {
// clear the background of all rows
var rows = document.getElementById('my_table').rows;
for(var i = 0; i < rows.length; i++) {
rows[i].style.backgroundColor = '';
}
// set background of clicked row
this.style.backgroundColor = 'purple';
}
See an example.
In createCell change cell.setAttribute('onclick', 'alert("hello")');
to cell.setAttribute('onclick', 'alert("hello");this.parentNode.setAttribute("bgcolor", "purple")');
Figured out that if I set the clickcell function to the following, it will clear all cells that are colored. I thought I could call "this.style.backgroundColor" to change the cell color and then clear that cell with a "row.style.backgroundColor" turns out I was wrong.
function clickCell(e) {
var tr = document.getElementById('my_table').rows;
for(i=0;i<tr.length;i++)
{
for(k=0;k<tr[i].cells.length;k++)
{
tr[i].cells[k].style.backgroundColor = "";
}
}
this.style.backgroundColor = "blue";
}
精彩评论