How to change BGcolor screen using JS function?
I am trying to create a simple button to change the background color using a JS function. can't get it to work though ...
<!doctype html><html>
<head>
<title>Blank Page</title>
<script type="text/javascript">
var bg = function(){
var color = prompt("Please enter a color" , "Color");
return color;
}
</script>
</head>
<body>
<input type="button" value="Change BG" onClick="document.bgColor='bg()'">
</body>
</html>`
+3
source to share
4 answers
You need to remove ''
from 'bg()'
. Thus.
Please find document.bgColor has been deprecated since HTML4.0, so it did not recommend using it.
<script>
function bg(){
var color = window.prompt("Please pick color");
return color;
}
</script>
</head>
<body>
<input type="button" value="changeBG" onclick="document.bgColor=bg();"/>
</body>
</head>
+1
source to share