Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  46] [ 1]  / answers: 1 / hits: 15851  / 7 Years ago, thu, april 6, 2017, 12:00:00

I am trying to make an annual salary calculator using Javascript. Here is what I have so far:





<div id=fulldiv>
<p> Enter the following information to calculate your annual salary.</p>

<p>Hourly Wage:
<input type=text name=wage id=txt_wage value =0.00/></p>

<p>Hours Per Week:
<input type=text name=hours id=txt_hours value= 0.0/> <br/><br/>

<button value=calculate onclick=calcSalary()>Calculate</button></p>

<p id=results></p>

<script type=text/javascript>

function calcSalary() {
var wage_element = document.getElementById('txt_wage');
var wage = parseInt(wage_element.value);
var hours_element = document.getElementById('txt_hours');
var hours = parseInt(hours_element.value);
var calculate = wage_element * hours_element * 52;
document.getElementByID('results').innerHTML = calculate;
}
</script>




</div>





When I click the button, nothing happens. Any thoughts?


More From » javascript

 Answers
3

Some typos in there. I have slightly rewritten and simplified the code to ensure the a) your calculations are on the value of the inputs and b) you are using labels to provide the text relative to the inputs - not p's.





function calcSalary() {
var wage = parseFloat(document.getElementById('txt_wage').value);
var hours = parseFloat(document.getElementById('txt_hours').value);
var calculate = wage * hours * 52;

document.getElementById('results').innerHTML = calculate;
}

<div id=fulldiv>
<p> Enter the following information to calculate your annual salary.</p>
<label for=txt_wage>Hourly Wage:</label>
<input type=text name=wage id=txt_wage value =0.00/>

<label for=txt_hours>Hours Per Week:</label>
<input type=text name=hours id=txt_hours value= 0.0/>
<br/><br/>
<button value=calculate onclick=calcSalary()>Calculate</button>
<p id=results></p>
</div>




[#58241] Wednesday, April 5, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;