views:

56

answers:

3

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> 
+2  A: 

In createCell change cell.setAttribute('onclick', 'alert("hello")'); to cell.setAttribute('onclick', 'alert("hello");this.parentNode.setAttribute("bgcolor", "purple")');

Vinay B R
That does work, however, it changes the color of the 3rd row to purple. I only want the clicked cell to change color. This.parentNode.parentNode.setAttritube will color the entire table.
cameron213
then use this.setAttribute and also i used only this.parentNode thinking you wanted to change color of entire row. this refers to currentcell since that is the element on which the onclick event is registered
Vinay B R
+1  A: 

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.

Anurag
How do you change the background color on only the clicked cell? Thanks for the help
cameron213
`this` refers to the `<td>` element in this function. Don't call `this.parentNode` which will get the `<tr>` element, and simply do `this.style.backgroundColor = 'purple'`. Updated answer.
Anurag
I see that if you set this.parentNode.style.backgroundColor = 'purple'; to this.style.....it does only change the cell's background color. thanks again
cameron213
As far as removing the background color from the cell how do you manage that? I've tried just about everything I can think of. Tried using a loop like I did to populate the elements of the table.
cameron213
See the linked example. It removes the background color of all rows.
Anurag
Hmm...Is it browser specific? If I run the example in FF, and add the changes I can click a cell, and change the color. If I click another cell it changes the color leaving the other one still colored. And so on until all three are colored. If I run this in IE I get some object error and it doesn't even print the table. Not to worried about the IE error, I guess addEventListener is FF only, not sure..its not working for me. ha ha
cameron213
A: 

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";
}
cameron213