[Javascript] Syntax help: Assign padding to an element based on the width of its parent

Barney Carroll barney at textmatters.com
Tue Sep 18 04:30:30 CDT 2007


Hi list!

I'm not all that hot with Javascript (which is why I tend to try for 
really petty scripts like this), and strongly suspect my syntax is 
horribly wrong at some point here. I don't get any errors, but the 
script doesn't have any of the effect I want either:

window.onload = function () {
   forms = 
document.getElementById('region-content').getElementsByTagName('form');
   for (var i=0, j=forms.length; i<j; ++i) {
     var formwidth = forms[i].getOffsetWidth;
     var legends = forms[i].getElementsByTagName('legend');
     for (var k=0, j=legends.length; k<j; ++k) {
       legends[k].style.paddingRight = (formwidth - 
legends[k].gettOffsetWidth - 12) + 'px';
     }
   }
}


I'm using this script to make it appear that legend elements fill the 
width of their containers the same way headers do, which is impossible 
with pure CSS. The logic goes that for every legend, I subtract its 
width from the width of its containing form, and then a further 12px 
(the combined horizontal padding on my forms), to give the amount of 
right padding needed to make up the width... But it's not working!

Any help please?


Regards,
Barney



More information about the Javascript mailing list