Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Error whilst changing the colour of buttons "TypeError: null is not an object (evaluating 'document.getElementById('button'+x+'').style')"

I am trying to change the colour of multiple buttons in my html website, however I am running into errors when it is run.

HTML:

<body style="background-color:rgb(216, 216, 216);">
             
            <button id=button0 type="button" class="block" onclick="likeAction();">LIKE</button>
            
            <button id=button1 type="button" class="block" onclick="likeAction();">LIKE</button>
            
            <button id=button2 type="button" class="block" onclick="likeAction();">LIKE</button>
             
            <button id=button3 type="button" class="block" onclick="likeAction();">LIKE</button>
            
            <button id=button4 type="button" class="block" onclick="likeAction();">LIKE</button>
            
            <button id=button5 type="button" class="block" onclick="likeAction();">LIKE</button>
</body>

JS:

async function likeAction(){
                for (let x = 0; x < 6; x++) {
                    document.getElementById('button'+x+'').style.backgroundColor = 'salmon';
                }
            }

ERROR:

Unhandled Promise Rejection: TypeError: null is not an object (evaluating 'document.getElementById('button'+x+'').style')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Change

id=button0

->

id="button0"

and change

document.getElementById('button'+x+'') 

->

document.getElementById('button'+x)
about 4 years ago · Juan Pablo Isaza Report

0

You need to change some code, as shown below.


First, you need to change your id attribute in your HTML to be like this, so that it isn't invalid.

<button id="<id>"></button>

Basically, surround the ID text with quotes.


Also, in your JavaScript, change your getElementById function to this.

document.getElementById("button" + x);

This is because the extra empty string is redundant.


In summary, you were getting the error as the HTML didn't understand the ID, as you hadn't typed it in properly.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!