CJ Cohorst
CJ Cohorst

Reputation: 115

Changing the background color of a page

I am trying to make a command line, where when you type a command, such as 'bg green', the background turns green. Everything works okay except for when you type 'bg green', and press enter, nothing happens. The background stays black. This is my code:

<html>  
<body style="background-color:black" id="a">
<div style="width:100%;bottom:0px;height:30px;position:absolute;"></div>
<div id="commandline" onkeydown="if (event.keyCode == 13) command(); " style="-webkit-transform:rotate(0deg);font-family: 'Kelly Slab', cursive;z-index:2;background-color:black;height:36px;width:100%;bottom:0px;position:absolute;color:white;left:0px;font-size:20px;">
&gt
<input style="font-family:'Kelly Slab';color:white;height:100%;width:100%;background-color:black;border:none;position:absolute;font-size:30px;left:20px;top:0px;" id="cmdinput"></input>
<div id="commands"  style="z-index:1;background-color:black;height:100%;width:100%;bottom:36px;position:absolute;left:0px;"></div>
</body>
</html>    

<script type="text/javascript">

function command(){
if(document.getElementById('cmdinput').value.substring(0,9) == "bg green"{
document.getElementById('a').style.backgroundColor = 'green';
}
}

</script>    

I think the issue is with the javascript, but I'm not sure. Does anyone know what the issue is?

Upvotes: 0

Views: 134

Answers (2)

Alex
Alex

Reputation: 11579

Missing closing bracket (after "bg green").

if(document.getElementById('cmdinput').value.substring(0,9) == "bg green") {
document.getElementById('a').style.backgroundColor = 'green';
}

Upvotes: 4

Automate This
Automate This

Reputation: 31364

Try this instead:

document.body.style.background = 'green';

Upvotes: 2

Related Questions