Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:29:58+00:00 2026-05-16T22:29:58+00:00

I am creating a dynamic table of three columns and multiple rows. I want

  • 0

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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:29:59+00:00Added an answer on May 16, 2026 at 10:29 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What choices do I have for creating stateful dynamic content in an ASP.Net web
So I was thinking about creating a dynamic sql question, meaning that i want
which method do you prefer for creating dynamic sql queries? formating or streaming? Is
To the best of my knowledge, creating a dynamic Java proxy requires that one
When creating a class library in C++, you can choose between dynamic ( .dll
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select
When creating a web application, and lets say you have a User object denoting
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
Creating an XPathDocument with referenced DTD sometimes throws a web exception. Why?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.