george Posted November 27, 2010 Share Posted November 27, 2010 I have a function called by an event. On arrival to my function I want to get and set the width value of a div object. But I can’t seem to get it, or set it. Here is what I have tried using "coverup" as my object ID: <style>#coverup { position:absolute; top:0; left:0; width:120px; height:auto; }</style><script>document.getElementById('coverup').width </script><html> <div id="coverup"></div></html> I think my problem is not knowing the proper DOM script to get the value of the width of the object ‘coverup’. Thanks Link to comment Share on other sites More sharing options...
jeffman Posted November 27, 2010 Share Posted November 27, 2010 The DOM property is element.offsetWidth . I would try that first and see if it does what you want.If you want the width as it is defined by CSS, the procedure can be trickier. You may want to read this. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 27, 2010 Share Posted November 27, 2010 use scrollWidth to get width of element including padding, but not including any border width.use offsetWidth to get width of element including padding, and any border width.To assign a new width with document.getElementById('coverup').style.width="240px";If you retrieve the value using whatever method and assign the value to a variable, remember to add 'px' unit text at end.var currentwidth = document.getElementById('coverup').scrollWidth;document.getElementById('coverup').style.width=(currentwidth+120)+"px"; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.