how do I change the background color using Javascript?

Asked

Viewed 39 times

1

i tried this code but it was not. I am beginner and who power is available to take other doubts I will be very grateful.

<script type="text/javascript">
    document.getElementbById(fundo).style.backgroundColor = "black";
</script>
</head>

<body id="fundo">
</body>

2 answers

4

Your code is almost correct. The problem is that you wrote the method name getElementById wrong way and forgot the quotes in the ID. The code should be:

document.getElementById("fundo").style.backgroundColor = "black";

Follow an example:

document.getElementById("fundo").style.backgroundColor = "black";
/* estilo apenas para o texto aparecer quando mudar a cor de fundo */
#fundo {
 color: #fff;
}
<div id="fundo">
  Fundo
</div>

0

@Rafael Silva you can use:

Document.querySelector('#background');

Just follow the example:

OnClick('#Fundo')=function()
{
       document.querySelector('#fundo');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.