• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Javascript display value of function

Hi,

I have followed a guide online that performs a real time calculation using javascript

http://www.javascript-coder.com/javascript-form/javascript-calculator-script.phtml

I have modified the formto so that it calculates an employees potential bonus for the year. This works ok however I want to show a break down of  the values used below.. i.e getsalary()

I used the following javascript

function showsalary()
{
    
    var salary = getsalary();
    var salary2 = document.getElementById('dsalary');
    salary2.innerHTML = salary;
}

Open in new window


and the following html -
<center><table border="1" WIDTH="330" class="header1">
<tr>
<td id="dsalary"><center>Your Salary</center>
</tr>
</td>
</table></center>
<br>
<br>

Open in new window


This should display the salary that has been entered in the form. However it just remains blank.

Can someone please help? I have attached the javascript and html used for your reference.
formcalculations.js
style.css
cakeform.html
0
Dan560
Asked:
Dan560
  • 4
  • 4
  • 2
1 Solution
 
nap0leonCommented:
I don't see anywhere where you are calling the function "showsalary()".

BTW, your HTML has several errors which may be causing it to not work right:
the closing </td> in your "Your Salary" table should go before the </tr>.

Missing a closing > (two locations):
in salary: <input type="text"  name="salary" id="salary"
in aprrating: </select


Also, you have an extra </select in CSR (missing a >, but since the whole thing is extra, it should be rmeoved.
0
 
Dan560Author Commented:
Ok I used the following code in javascript

 var salary2 = document.getElementById('dsalary');

I then used code in my html

<td id="dsalary"><center>Your Salary</center>

however I am open to any help as I am by no means an expert have really self taught myself with the help of Google.

Apologies for the poor coding.
0
 
rkeith2412Commented:
There are two issues.

1.  You never tell your page to run the show salary function.

2.  In your function getsalary() howmany = parseInt(salary.value); should be howmany = parseInt(quantity.value);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rkeith2412Commented:
I would add the showsalary() tot he end of calculateTotal()
0
 
nap0leonCommented:
In your HTML:
onload=hideTotal()

Open in new window

Tries to hide a piece of HTML that does not exist ('totalPrice').  I removed it.

In your JS file:
howmany = parseInt(salary.value);

Open in new window

salary is not defined, so you cannot get the value of it.
looks like a couple lines above needs to be changed to say 'salary' instead of 'quantity'
    var salary = theForm.elements["salary"];

Open in new window


There may have been a few other small edits I made along the way... I think there were two "body" tags too.
cakeform.html
formcalculations.js
0
 
Dan560Author Commented:
but my function calculatetotal() is working I just want to show the getsalary() value on another part of the page without changing anything to the calculatetotal() function.
0
 
Dan560Author Commented:
nap0leon did not see your comment. will try your suggestions now
0
 
nap0leonCommented:
Using the pages I posted, please test them out and then detail what changes you would like to make.

Right now, when you select any of the drop-downs, the total pay including bonus displays in as "YOUR ANNUAL BONUS COULD BE: €101250" and the salary (the same number that was entered in the salary input at the top of the page) displays in the "YOUR SALARY" area.

I would presume you actually want bonus amount, and not 'total compensation' displayed for "Your annual bonus could be..." and where you have salary showing at the bottom of the page is where you actually want total compensation, but that's just a guess.
0
 
Dan560Author Commented:
Perfect it works exactly the way I want it.

Many thanks
0
 
nap0leonCommented:
Love it when posts stream in at the same time. ;o)
Let us know if you need any help adjusting which values appear in each section.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now