I've commented the code in one of your functions:
Code:
function calHeight(){
var obj = document.getElementById('newwidth'); // returns object (HTML input element)
var newW = obj.value; // returns string (input's value)
var oldW = document.getElementById('widthpx'); // returns object (HTML input element)
var oldH = document.getElementById('heightpx'); // returns object (HTML input element)
var newH = Math.round(newW/oldW*oldH); // a string divided by an input object multiplied by an input object will always be NaN
var obj = document.getElementById('newheight'); // returns object (HTML input element)
obj.value = Math.round(newH); // Math.anything(NaN) is still NaN
}
Now a string is not a number, but when it is comprised only of characters that represent a number, javascript will usually do automatic type conversion on it, so you should be OK there.
However, an HTML input element object is never a number. What you want in those spots is the value of the input object, like you've done with newW. In fact, if you are only referencing each object once in the code, there's no reason to make so many variables. The main thing though is to make sure you are using numbers, or strings that can easily convert to numbers:
Code:
function calHeight(){
var newW = document.getElementById('newwidth').value;
var oldW = document.getElementById('widthpx').value;
var oldH = document.getElementById('heightpx').value;
var newH = Math.round(newW/oldW*oldH);
document.getElementById('newheight').value = newH;
}
Something should probably be done to make sure the string values can be converted to numbers and either alert the user, or substitute some default number if they cannot. But assuming the user enters numbers only, the above will work. Also, it could be further simplified code-wise, but I didn't want to change it past all recognition.
Bookmarks