d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy Javascript Question
Add Reply New Topic New Poll
Member
Posts: 1,144
Joined: Jul 25 2012
Gold: 0.00
Dec 1 2012 06:02pm
I don't get why this isn't working...I just created a simple form for people to enter their height/weight into and it will convert it into cm/kg...but when I click the button nothing is happening and I'm utterly confused as to why


<form name="calculator" method="post" action="">
<p>
<label for="height">Height(in)</label>
<input type="text" name="height" id="height" /> <span id="result1"> </span>
</p>
<p>
<label for="weight">Weight(lbs)</label>
<input type="text" name="weight" id="weight" /> <span id="result2"> </span>
</p>
<input type="button" name="calculate" value="Calculate" id="calculate"/>
</form>


<script type="text/javascript">
var btn = document.getElementById('calculate');
btn.onclick = function() {

var he = parseInt(document.getElementById('height').value);
var we = parseInt(document.getElementById('weight').value);

var result1 = document.getElementById('result1');
var result2 = document.getElementById('result2');
var error = [];
if(isNaN(he)){
error.push('Please enter a number into height field');
}
if(isNan(we){
error.push('Please enter a number into weight field');
}
if(error.length > 0) {
result1.innerHTML = error.join(', ');
}
else {
result1.innerHTML = 'Your height in CM is: ' + he * 2.54;
result2.innerHTML = 'Your weight in KG is: ' + we * 0.453592;
}
};
</script>

This post was edited by Hypertrophy on Dec 1 2012 06:07pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2012 06:09pm
im not big on js so i dont know when that script block gets executed. i would try setting the onclick method directly inside the html control, defining the function in the header, and remove the method=post on the form.

This post was edited by carteblanche on Dec 1 2012 06:25pm
Member
Posts: 1,144
Joined: Jul 25 2012
Gold: 0.00
Dec 1 2012 06:11pm
Quote (carteblanche @ Dec 1 2012 07:09pm)
im not big on js. i would try setting the onclick method directly inside the html control and remove the method=post on the form.


I fixed it -.- the second if(isNaN) the second N wasn't capitalized -.- this is why I despise any type of programming lol
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2012 06:12pm
Quote (Hypertrophy @ Dec 1 2012 08:11pm)
I fixed it -.- the second if(isNaN) the second N wasn't capitalized -.- this is why I despise any type of programming lol


shouldn't you get an error when functions dont exist?
Member
Posts: 1,144
Joined: Jul 25 2012
Gold: 0.00
Dec 1 2012 06:14pm
Quote (carteblanche @ Dec 1 2012 07:12pm)
shouldn't you get an error when functions dont exist?


Not when you're editing in notepad++ I guess :/ I'm sure if I was using VIM or something it would post an HTTPerror

I just figured the logic was off somewhere, so I hadn't checked httperrors on the server

This post was edited by Hypertrophy on Dec 1 2012 06:14pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2012 06:16pm
IDE should have nothing to do with it unless you mean a tool that checks for functions/syntax/etc. i was referring to the browser. i figured you'd see a message (possibly via firebug) that a function didnt exist
Member
Posts: 1,144
Joined: Jul 25 2012
Gold: 0.00
Dec 1 2012 06:18pm
Quote (carteblanche @ Dec 1 2012 07:16pm)
IDE should have nothing to do with it unless you mean a tool that checks for functions/syntax/etc. i was referring to the browser. i figured you'd see a message (possibly via firebug) that a function didnt exist


ah yah our babbage server has an httperrors tool that would probably post it, but running it in chrome it just ignored it and when clicking the calculate button nothing would happen
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Dec 1 2012 06:55pm
You need to use document.ready before executing the script, or else the script will execute before the html finishes rendering... :P
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Dec 1 2012 07:30pm
Quote (StormHasHe @ Dec 1 2012 06:55pm)
You need to use document.ready before executing the script, or else the script will execute before the html finishes rendering... :P


yep.

Code
$(document).ready(function(){

YOUR CONTENT GOES HERE

});


This post was edited by PixileDust on Dec 1 2012 07:30pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll