Reza Arbabi
Reza Arbabi

Reputation: 47

How to change the color of a div tag in html with javascript code

How to change the color of a div tag in HTML with JavaScript code

Upvotes: 0

Views: 63

Answers (2)

Mayank Gupta
Mayank Gupta

Reputation: 202

<div id="myDiv">
  //your code goes here
</div>
<script>
  document.getElementById("myDiv").style.backgroundColor = "red";
</script>

You have to trigger this is somehow like on body load or on button click

Upvotes: 1

Mohammad Sultani
Mohammad Sultani

Reputation: 9

// selecting the html element using id

var div = document.getElementById("apple");
div.style.color = "red";
<div id="apple">
  <p>some text</p>
 </div>

Upvotes: 0

Related Questions