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 7671671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:06:45+00:00 2026-05-31T16:06:45+00:00

I am having a javascript problem. What I am trying to do is set

  • 0

I am having a javascript problem. What I am trying to do is set the colour of every cell in a table to a chosen colour once a reset button has been clicked.

This is what I have so far:

function resetColour(objName)
{  
    var arr = new Array();
    arr = document.getElementsByName(objName);

    alert("total objects with name \"free\" = \n" + arr.length);

    for(var i = 0; i < arr.length; i++)
    {
        arr(i).style.backgroundColor = "#FAF4E3";
    }
}

The end of my html looks like this:

                <tr>
                    <td onclick="tdOnclick(this)"><center>17:00 - 18:00</center></td>
                    <td onclick="tdOnclick(this)"><center><input type="checkbox" name="free" value="mon17"></center></td>
                    <td onclick="tdOnclick(this)"><center><input type="checkbox" name="free" value="tue17"></center></td>
                    <td onclick="tdOnclick(this)"><center><input type="checkbox" name="free" value="wed17"></center></td>
                    <td onclick="tdOnclick(this)"><center><input type="checkbox" name="free" value="thu17"></center></td>
                    <td onclick="tdOnclick(this)"><center><input type="checkbox" name="free" value="fri17"></center></td>
                </tr>
                <tbody>
            </table>
            <input type="reset" name="reset" value="Reset" onclick="resetColour('free')">
            <input type="submit" name="submit" value="Submit">

What am I doing wrong that is making this not work?

EDIT: This piece of javascrip might be messing things up for me. How can I edit this so that it will change the colour of the whole td?

function tdOnclick(td) {
    for(var i = 0; i < td.childNodes.length; i++) {
        if(td.childNodes[i].nodeType == 1) {
            if(td.childNodes[i].nodeName == "INPUT") {
                if(td.childNodes[i].checked) {
                    td.childNodes[i].checked = false;
                    td.style.backgroundColor = "#FAF4E3";
                } else {
                    td.childNodes[i].checked = true;
                    td.style.backgroundColor = "#E1E1E1";
                    input.style.backgroundColor = "#E1E1E1";
                }
            } else {
                tdOnclick(td.childNodes[i]);
            }
        }
    }
}
  • 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-31T16:06:46+00:00Added an answer on May 31, 2026 at 4:06 pm

    so you want the <td>‘s to change color based on the name you pass? (not the <input>, not the <center>)

    function resetColour(objName){  
    
        //get element by name (as requested by post)
        var arr = document.getElementsByName(objName);
    
        //store length in variable
        //some say accessing .length in a loop condition adds overhead 
        var arrLen = arr.length;
    
        //loop through all found elements    
        for(var i = 0; i < arrLen; i++){
    
            //InputElement.CenterTag.TdElement.style.backgroundColor = foo
            arr[i].parentNode.parentNode.style.backgroundColor = "#F00";
        }
    }
    
    function tdOnclick(obj){
        obj.style.backgroundColor = "#F00";
    }
    

    you might wanna avoid using <center>, besides that it’s deprecated, it’s the cause of the additional .parentNode in the code (totally unnecessary). use CSS to center content instead.

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

Sidebar

Related Questions

I'm having a problem while trying to dynamically change a table cell's styling class
I am having a problem accessing the ViewData object through javascript. I have set
I'm having a weird problem with some JavaScript/DOM code I've been playing with. I'm
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
I'm having a bit of a problem because neither Javascript nor ActiveX (written in
just a quick question. I'm having a problem with divs with onclick javascript within
I'm having difficulty with Javascript instance variables. I'm trying to make an easy way
Hi I am trying to set focus to a button when it is tabbed
I'm having an annoying problem trying to get an html page with a google
I'm trying to translate this line of Javascript code to Jquery. The problem i'm

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.