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
asked Feb 22, 2013 at 23:28
-
1What are you trying to do?tim.baker– tim.baker2013年02月22日 23:30:49 +00:00Commented Feb 22, 2013 at 23:30
5 Answers 5
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"
answered Feb 22, 2013 at 23:31
Sign up to request clarification or add additional context in comments.
Comments
document.getElementById("sharebar").setAttribute("style", "width:0px;");
answered Feb 22, 2013 at 23:30
3 Comments
sachleen
I don't like this because it'll overwrite any other styles you apply to the element.
Adisesha
agree..didn't think of that
sachleen
@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.
document.getElementById("sharebar").style.width = "0";
answered Feb 22, 2013 at 23:31
Comments
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');
answered Feb 22, 2013 at 23:37
Comments
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";
answered Feb 22, 2013 at 23:33
Comments
lang-js