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

408
Views
Trying to change value of buttons

I'm trying to change the value of a button when I click on it, so what I did was I created a function in JavaScript where I call the ID of the element by getting the ID of the button and through style.backImage try and change it.

But it shows this error:

Uncaught TypeError: Cannot set properties of null (setting 'value')

This is the code of the button:

<button id="id1" style="margin: 17px;" onclick="mark()"> . </button>

And the JavaScript function is the following:

function mark(){
  document.getElementById(id1).style.backgroundImage = "url('cruz.webp')";
}

I've tried moving the JavaScript document from the head to body in HTML but hasn't done any good.

I know it must be really simple but I appreciate your help.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The issue with your code is in the JavaScript section of your code, with the function getElementById().


You need to change the parameter passed in getElementById() to be like below.

document.getElementById("id1");

The difference is that your code doesn't have id1 surrounded in quotes, but this answer has "id1" surrounded in quotes.


The reason your error is appearing is that the variable id1 (which is what JavaScript thinks) isn't defined, so getElementById() is returning null, which is what the error states (shown below).

Uncaught TypeError: Cannot set properties of null (setting 'value')

When you surround id1 with quotes, JavaScript should find the element and return an Element, which should stop the error from appearing and make your code work correctly.

over 4 years ago · Santiago Trujillo 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!