-2

New to JavaScript and HTML. I'm trying to change the background color in my HTML code through JavaScript.

The only thing that I need to happen is to change the background color on divMain below from black to white using only JavaScript and keeping the HTML code the same

<style>

#divMain {
position:absolute;
left:0px;
top:0px;
width:320px;
height:460px;
background-color:black;
-webkit-user-select:none;
-webkit-text-size-adjust:none;
}

</style>

</head>

<body 
onload="body_load();" 
>

<div 
id="divMain">
</div>

The only things that I have in the JavaScript code is this:

function body_load() {
}
5
  • 2
    Have you tried anything? Commented Feb 1, 2014 at 17:46
  • Thanks, that helps a little bit but I don't see how to change it inside of a div tag. Only how to change the background of the body Commented Feb 1, 2014 at 17:47
  • This question appears to be off-topic because it is about absolute basics, that you should read up on in some tutorial or other. Commented Feb 1, 2014 at 17:49
  • This question has an answer to set it for a specific element: stackoverflow.com/questions/3319/… Commented Feb 1, 2014 at 17:50
  • why you need this? you can't modify the bg color with CSS? the link above (answer from @ryanulit ) solve your question. Commented Feb 1, 2014 at 17:50

2 Answers 2

2

If you want to change the background of an element, you can use this:

document.getElementById("divMain").style.backgroundColor = "#FF0000";

It will change the div-Background to red. But why do you want to make this with javascript?

Solve it with inline-CSS:

<div id="divMain" style="background-color: red"></div>
Sign up to request clarification or add additional context in comments.

4 Comments

Nevermind figured it out. thank you!
there was a spelling-error. I updated it
Solve it with HTML I'd call that inline CSS.
@MarkShimala If the answer worked for you, please accept it. If not, post your own answer.
0

document.getElementById("divMain").style.backgroundColor="red" e.g.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.