There are several issues with the code, and partly difficult to track down, but you can get started with debugging by using Developer Tools in your browser. You typically enter them by pressiin the F12 key. The “Console” pane of the tools will show JavaScript errors and other diagnostics.
You will then first see that the colon :
causes a syntax errors. In JavaScript, you need to use the assignment operator =
.
Having fixed this, you will see that clicking on the button will cause an error message about body.style
being undefined. That’s because you have called the document.getElementsByTagName
function that returns an array of elements, not a single element. The simplest fix to use just document.body
, which refers to the body
element.
The remaining bug is more tricky and causes somewhat varying diagnostics in browsers, like “TypeError: size is not a function” in Firefox. The reason is that the invocation size()
is inside an event handler in the <input>
tag, and there the implied context is the <input>
element node. And that node has the size
property, which reflects to the HTML markup attribute size
. This property is defined even though the markup does not have an explicit attribute; it is then defaulted to a browser-dependent value, typically 20. Anyway, in this context the identifier size
denotes that property, which has a numeric value, not a function.
There are two simple ways to fix this bug. You can change the invocation size()
to window.size()
, thereby referring to the globally defined identifier size
(it is global, i.e. a property of the window
object, since you define a function with that name at the topmost level). Alternatively, you can change the function name to something else, just avoiding anything that could be a property of the <input>
element node. You could use Size
for example, or changeBackgroundPosition
. So in HTML you could have
<input type="button" value="Change" onClick="changeBackgroundPosition()">
and in JavaScript
function changeBackgroundPosition() {
document.body.style.backgroundPosition = "0 30px";
}