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

  • Home
  • SEARCH
  • 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 8550967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:05:18+00:00 2026-06-11T14:05:18+00:00

I have a page where the user inputs a color and I call the

  • 0

I have a page where the user inputs a color and I call the onClick method to change the color of the individual cells of the table. However, when I click any cell, only the last cell (cell3 in this case) will change color. What am I doing wrong?

I get the error:

Message: ‘document.getElementById(…)’ is null or not an object
Line: 24
Char: 4
Code: 0

My code is:

    <html>

    <body>
    <input type='text' id='userInput' value='yellow' />

    <table border="1"> 
        <tr>
            <td id="1">cell1
            </td>
        </tr>
        <tr>
            <td id="2">cell2
            </td>
        </tr>
        <tr>
            <td id="3">cell3
            </td>
        </tr>

    </table>

    <script type="text/javascript">
    for(var i = 1; i <= 3; i++){
        document.getElementById(i).onclick = function(){
        var newColor = document.getElementById('userInput').value;
            document.getElementById(i).style.backgroundColor = newColor;
        }
    }
    </script> 
    </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-06-11T14:05:19+00:00Added an answer on June 11, 2026 at 2:05 pm

    Change your HTML to this: An ID must start with an an alpha character. It is not valid to start with a number.

    <table border="1"> 
        <tr>
            <td id="td1">cell1
            </td>
        </tr>
        <tr>
            <td id="td2">cell2
            </td>
        </tr>
        <tr>
            <td id="td3">cell3
            </td>
        </tr>
    
    </table>
    

    This is a very common Javascript issue: All the code shares the value of i which is 3 at the end of the loop. You can solve it by using another helper function like this:

    function changeIt(i) {
      // This inner function now has its own private copy of 'i'
      return function() {
        var newColor = document.getElementById('userInput').value;
          document.getElementById("td" + i).style.backgroundColor = newColor;
      }
    }
    
    for(var i = 1; i <= 3; i++){
        document.getElementById(i).onclick = changeIt(i);
    }
    

    It can also be written using an anonymous function, but those are harder to read.

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

Sidebar

Related Questions

I have a page that allows for used input, when the user inputs his/her
I have an inputText and a commandButton in a JSF page. The user inputs
I have form in my html page <form id=login_form method=POST action=index.php> <table> <tr> <td><label
I have a form on a page where the user has inputs to edit
I have a Login page that captures User input like this. MD5calc ss =
I want to have a form on my page take user input, a URL
I have a web page that prompts for user input via DropDownLists in some
I have a page where I need to capture input from a user after
I have a page that dynamically creates (using jQuery) Input boxes for the user.
asp c# i have a page where user selects an item and its quantity

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.