Well, I think you've got a bit of a mistake in your code. The line:
document.getElementById("mainTable") = tableHeight;
Should actually be:
document.getElementById("mainTable").style.height = tableHeight;
Here's a JSFiddle with the fixed code, seems to give you what you need. Let me know if you have any questions!