0

I have been working on getting the background color of a div and add padding to the div i want to do this WITHOUT JQUERY I have the html but no javascript yet. I have been trying the other code from google but it will not work. What i am trying do not work so I am looking for someone who can get me the code for this project. THIS NEEDS TO BE

WITHOUT JQUERY

HTML CODE:

<div style="background: green">
     HELLO WORLD
</div>

Thank You.

  • 1
    Without using jquery acheving this can be possible using sass and less or if you want yo get the background color using jquery than you can do it like div.style.backgroundColor; – Anil Panwar Dec 20 '15 at 17:16

3 Answers3

2

ELEMENT.style.backgroundColor will give you the CSS-Attribute of the Element. (You can also use it to set the color)

To set the padding use ELEMENT.style.padding = "10px"

alert(document.getElementById("myDiv").style.backgroundColor)

document.getElementById("myDiv").style.padding = "100px"
<div id="myDiv" style="background: green">
     HELLO WORLD
</div>
CoderPi
  • 12,985
  • 4
  • 34
  • 62
1

For this particular example:

<div style="background: green">
    HELLO WORLD
</div>

You can just pick it up from the style object:

document.querySelector('div').style.background;

If it's applied through a style tag or a stylesheet then you can use:

window.getComputedStyle(document.querySelector('div')).background
MinusFour
  • 13,913
  • 3
  • 30
  • 39
0

Try this.

document.getElementsByTagName('div')[0].style.backgroundColor
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55