...

View Full Version : Javascript problem



Feartheyankees
08-23-2011, 06:40 AM
I'm having a problem with a javascript code I put together. For all of the PHP, CSS, HTML that I know, well it doesn't carry over to JavaScript, so my attempt at trying this is probably a very bad attempt.

Anyways, what I'm trying to accomplish with this code is to get the height of one table and set the padding-bottom to another table, to keep page dimensions even.


<script type="text/javascript">
var divArray = document.getElementById('tablecontent').offsetheight;
//Subtracting 442 because that's the height of the table. I'm just trying to add onto the bottom.
var subtraction = divarray - 442;
var tablesidebar = document.getElementById('tablesidebar');
tablesidebar.style.paddingbottom = subtraction + 'px';
</script>


Any help to get this working is well appreciated.

Thanks,
Jeremy

devnull69
08-23-2011, 12:38 PM
Your work is not bad. But there are two things to mention:

1. Javascript is case-sensitive. So it is supposed to be offsetHeight, divArray and paddingBottom
2. To make sure the addressed elements like "tablecontent" already exist in the DOM, wrap your code in an window.onload event handler


window.onload = function() {
// your code goes here
};

Feartheyankees
08-23-2011, 05:03 PM
I took your advice and put in the window.onload and proper casing. However, there's still no luck. However, I did check Mozilla's error console, it's telling me that there's a syntax error at
var divArray = document.getElementById('tablecontent').offsetHeight;

The code now looks like this

<script type="text/javascript">
window.onload = function () {
var divArray = document.getElementById('tablecontent').offsetHeight;
var subtraction = divArray - 442;
var tablesidebar = document.getElementById('tablesidebar');
tablesidebar.style.paddingBottom = subtraction + 'px';
};
</script>

EDIT: Nevermind, had an element off, thanks.

devnull69
08-23-2011, 07:06 PM
It's not a syntax error, it's a logical error. offsetHeight is not a property of an element but rather a property of an element's style


var divArray = document.getElementById('tablecontent').style.offsetHeight;

Note: You will only get a value for style.offsetHeight if the property has been set using the style attribute. Otherwise you will have to use a combination of getComputedStyle/getPropertyValue

auslin
08-24-2011, 04:46 AM
This should be OK:


var divArray = document.getElementById('tablecontent').offsetHeight;

I always understood the offsetLeft, offsetHeight, offsetWidth and offsetHeight properties to be read-only properties of all HTML element objects, and not properties of their associated style objects.
https://developer.mozilla.org/en/DOM/element.offsetHeight

Attempting to read or set .style.offsetHeight, style.offsetLeft, etc would be expected to generate an error.

devnull69
08-24-2011, 01:25 PM
Ok, you are right. Nevertheless, the statement should not have produced a Syntax Error. The only other obvious problem would be that there is no element with id="tablecontent"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum