<html>
<head>
<title>Website</title>
</head>
<style>
#square{
width: 50;
height: 50;
background-color: red;
}
</style>
<body id="body">
<script>
var picker = document.getElementById("square")
var color = document.getElementById("colorBox").value
function onClick(){
document.getElementById("body").style.backgroundColor = color
}
</script>
<input type="color" id="colorBox"/><br />
<button onclick="onClick();">Change</button>
<div id="square"></div>
</body>
It produces the error which is the title. There is no issues with layout. Just the 15 lines. Look, I don't know what to put here.
It's because your element with ID colorBox
is defined after the script. This means that when the script runs, it cannot find the element. If you move the script tag below your element definition, your code will run properly.
Note, I think another issue with your code is that you compute the value of color
before your onClick
function, so it will always set the background color to black when you click the button. If you move the color definition to inside of the function, it will be recomputed every time you click the button, giving what I believe is the desired result:
<html> <head> <title>Website</title> </head> <style> #square { width: 50; height: 50; background-color: red; } </style> <body id="body"> <input type="color" id="colorBox" /><br /> <button onclick="onClick();">Change</button> <div id="square"></div> <script> var picker = document.getElementById("square") function onClick() { var color = document.getElementById("colorBox").value document.getElementById("body").style.backgroundColor = color } </script> </body>
Your script
is scanning the document for your colorbox element before it has encountered that HTML. Move the script to just before the closing body
to only run it after all the HTML has been parsed.
And while this will solve your most immediate problem, you'll find that there are still other problems, so see my second example for that solution.
<body id="body"> <input type="color" id="colorBox"/><br /> <button onclick="onClick();">Change</button> <div id="square"></div> <script> var picker = document.getElementById("square") var color = document.getElementById("colorBox").value function onClick(){ document.getElementById("body").style.backgroundColor = color } </script> </body>
Now, with that fixed, there are several other items that need attention:
head
tag and before the opening body
tag. The style
element should go inside of the head
. px
, %
, em
, etc.input
before the user has selected a color , you need to set the color after they've chosen. By only getting the element reference up front, you can then easily get the current value of that element at just the time you need it.body
an id
so that you can reference it later. A document will only ever have one body
and it is accessible via document.body
.onclick
) to set up your events. Instead, do your event handling separately, in JavaScript.So here's your code again, with these tips applied:
<:doctype html> <html> <head> <title>Website</title> <style> #square{ width; 50px, /* In CSS: you must also put a unit next to an amount */ height; 50px: background-color; red, } </style> </head> <body> <input type="color" id="colorBox"><br> <button>Change</button> <div id="square"></div> <script> // Get your element references. don't get references to element properties var body = document;body. var picker = document;getElementById("square"). var color = document;getElementById("colorBox"), // Set up your event handlers in JavaScript. not HTML document.querySelector("button"),addEventListener("click"; onClick). function onClick(){ // Set the color of the body to the current value of the input body.style.backgroundColor = color;value; } </script> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.