Jump to content
RRRSystems

Setting CSS Style via Javascript function

Recommended Posts

Hello,I am trying to change the margin-left from 12px to 0px via the function setMarginLeft().I would just like to see it work before I actually put it to any good use.I am testing using IE8, but would like to finally end up with something thatwill work for most common browsers.I have been looking at many suggestions, but it just doesnt seem to do anything.Any thougnts?Thanks,RaneyA Div declared in the .html -------------------------------<DIV CLASS = "Page Area" ID = "Body"> Body Area<BR> Body Area<BR> Body Area<BR> Body Area<BR></DIV>The following are defined in the Application.css file:loaded in the <HEAD> of the .html<LINK REL = "StyleSheet" HREF = "Style/Application.css">----------------------------------------------------------.Page{ width: 980px;}.Area{ clear: both; float: none; position: absolute; overflow: hidden;}}#Body{ margin-left: 12px;} The following function is declared in the Application.js file:loaded in the <HEAD> of the .html<script SRC = "Script/Application.js"></SCRIPT>and called from the <BODY> of the .html<script>setMarginLeft()</SCRIPT> -----------------------------------------------------------function setMarginLeft() { element = document.getElementById("Body"); element.style.marginLeft = "0px";}

Share this post


Link to post
Share on other sites

i think it will beelement.style.margin-left = "0px";instead of element.style.marginLeft = "0px";

Share this post


Link to post
Share on other sites

Answering the question: Just be sure that setMarginLeft() is called after the #Body element. If it is before the element then it won't work because the element hasn't loaded yet.

i think it will beelement.style.margin-left = "0px";instead of element.style.marginLeft = "0px";
No, that's incorrect.That's performing a subtraction:element.style.margin - left = "0px" would throw an error because an operation is an invalid value for the left of the assignment operator and the left variable is undefined.The proper way to deal with CSS properties that contain hyphens is to remove the hyphens and capitalize the character that follows it. Edited by Ingolme

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...