How can I make this checkbox work?

Head out to http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_backgroundcolor and use the code I've got below.

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>

<button type="button" onclick="myFunction()">Set background color</button>
<input type="checkbox" value="nelson_box" id="nelson" onclick="nelsonFunction()">

<script>
function myFunction() {
    document.body.style.backgroundColor = "red";
}

function nelsonFunction() {
if(document.getElementById("nelson").checked=true)
{
//alert("hello");
document.getElementById("nelsonBox").style.backgroundColor = "red"; 
}
if(document.getElementById("nelson").checked=false)
{
//alert("hello");
document.getElementById("nelsonBox").style.backgroundColor = "blue"; 
}
}
</script>
<br><br>
<div id="nelsonBox" style="width:150px; height:25px; background-color:blue;"></div>

</body>
</html>

Open in new window


What I want to do is create a situation for my user so when they click the checkbox, the box turns blue.  Right now, that works. But then I want a dynamic where when they uncheck it, it goes back to red.

How would I do that?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F IgorDeveloperCommented:
Use == in comparations in the if statements:

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>

<button type="button" onclick="myFunction()">Set background color</button>
<input type="checkbox" value="nelson_box" id="nelson" onclick="nelsonFunction()">

<script>
function myFunction() {
    document.body.style.backgroundColor = "red";
}

function nelsonFunction() {
if(document.getElementById("nelson").checked==true)
{
//alert("hello");
document.getElementById("nelsonBox").style.backgroundColor = "red"; 
}
if(document.getElementById("nelson").checked==false)
{
//alert("hello");
document.getElementById("nelsonBox").style.backgroundColor = "blue"; 
}
}
</script>
<br><br>
<div id="nelsonBox" style="width:150px; height:25px; background-color:blue;"></div>

</body>
</html>

Open in new window


Actually you have assigned true, and then assigned false to the checkbox.checked property.
(Always turning unchecked  that control)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Thank you, friend!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.