/    Sign up×
Community /Pin to ProfileBookmark

I can’t seem to get this to return a value. I can set the value and it works fine but retrieving the current value is not working. Any advice how to get this to work?

document.getElementById(divName).style.marginLeft

the divName is the name of the DIV I am trying to work with.

I’m stuck.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleFeb 07.2012 — To set a variable
[CODE][I]somevar[/I] = document.getElementById([I]divName[/I]).style.marginLeft;[/CODE]
To set a DOM element
[CODE]document.getElementById([I]divName[/I]).style.marginLeft = [I]somevar[/I];[/CODE]
Copy linkTweet thisAlerts:
@007JulienFeb 07.2012 — There is many problem with your question :

- At first, [I]divName[/I] without brackets has to be a javascript var,

- [COLOR="Silver"]Secondly, [I]marginLeft[/I] is to replace by [I]margin-left[/I],[/COLOR] // Sorry see aj_nsc correction !

- Thirdly, this property do not exist for a div without style attribute (style="margin-left:20px" for example - see this getStyle function to overcome this difficulty with or without css style),

- At least, you want probably work with this value and store it, like said JunkMale in a javascript var...
Copy linkTweet thisAlerts:
@aj_nscFeb 07.2012 — There is many problem with your question :

- Secondly, [I]marginLeft[/I] is to replace by [I]margin-left[/I],
[/QUOTE]


No this is not correct. You can do it either way:

<i>
</i>alert(document.getElementById('someId').style.marginLeft);
alert(document.getElementById('someId').style['margin-left']);


007 is correct about the inline style though, if you haven't declared margin-left (or margin) in an inline style attribute, then you need to use the getStyle function shown on quirksmode.
Copy linkTweet thisAlerts:
@Logic_AliFeb 07.2012 —  see this getStyle function to overcome this difficulty with or without css style[/quote]

To work properly that function needs the property format to be different for I.E. than other browsers. IE <9 needs "borderLeft" but browsers supporting [FONT=Courier New]getcomputedStyle[/FONT] expect "border-left" format. I wrote this version to attempt to use getComputedStyle first and convert the property format if necessary.

<i>
</i>
/* Multi-word properties must be specified in the format "prop-subprop" not "propSubprop".
Composite properties like 'border' should not be specified,
instead use individual properties like 'border-left', 'border-right' etc. */

function getStyle( elemId, styleProp )
{ <br/>
var elem = document.getElementById( elemId );

return window.getComputedStyle ?
( ( document.defaultView || window ).getComputedStyle( elem, null ).getPropertyValue( styleProp ) || "" )
:
elem.currentStyle ?
( elem.currentStyle[ styleProp.replace( /-(.)/g, function( a, b ){ return b.toUpperCase(); } ) ] || "" )
: "" ;
}
Copy linkTweet thisAlerts:
@horace_everettauthorFeb 07.2012 — You guys have been really helpful. My logic was way off, but you corrected me in a polite manner and got me thinking correctly. I appreciate it.
×

Success!

Help @horace_everett spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...