0

I want to change the padding and top, width to 0 using Javascript

Not able to do that.

Been trying things like:

 document.getElementById("sharebar").innerHTML="";  //This works
 alert(document.getElementById("sharebar").toString());
document.getElementById("sharebar").setAttribute("width", "0px"); //To remove formatting but this doesnt
1
  • 1
    What are you trying to do? Commented Feb 22, 2013 at 23:30

5 Answers 5

6

You're trying to set an attribute of the element, not its style. To change the style, you do something like this:

document.getElementById("sharebar").style.width = "0px"
Sign up to request clarification or add additional context in comments.

Comments

4
document.getElementById("sharebar").setAttribute("style", "width:0px;");

3 Comments

I don't like this because it'll overwrite any other styles you apply to the element.
agree..didn't think of that
@CodeMonkey of course it will work, but just be aware of that if you want to just change one of multiple styles applied to an element.
2
document.getElementById("sharebar").style.width = "0";

Comments

1

If you are trying to remove styles:

document.getElementById("sharebar").style.width = null;

or if you only care about supporting IE9+

document.getElementById("sharebar").style.removeProperty('width');

Comments

1

another way to do it is :

document.getElementById("sharebar").style.width = "0px";

for element that have - like z-index try the following :

document.getElementById("sharebar").style.zIndex = "10";

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.