Solved

Javascript display value of function

Posted on 2012-03-12
10
249 Views
Last Modified: 2012-03-12
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
Comment
Question by:Dan560
  • 4
  • 4
  • 2
10 Comments
 
LVL 18

Expert Comment

by:nap0leon
Comment Utility
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
 
LVL 2

Author Comment

by:Dan560
Comment Utility
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
 
LVL 5

Expert Comment

by:rkeith2412
Comment Utility
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
 
LVL 5

Expert Comment

by:rkeith2412
Comment Utility
I would add the showsalary() tot he end of calculateTotal()
0
 
LVL 18

Accepted Solution

by:
nap0leon earned 500 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:Dan560
Comment Utility
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
 
LVL 2

Author Comment

by:Dan560
Comment Utility
nap0leon did not see your comment. will try your suggestions now
0
 
LVL 18

Expert Comment

by:nap0leon
Comment Utility
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
 
LVL 2

Author Comment

by:Dan560
Comment Utility
Perfect it works exactly the way I want it.

Many thanks
0
 
LVL 18

Expert Comment

by:nap0leon
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now