Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  44] [ 5]  / answers: 1 / hits: 5515  / 10 Years ago, tue, march 18, 2014, 12:00:00

I have a function that takes in two parameters and returns the sum.



Now I'm trying ti create an form that takes in two numbers. When button is clicked it should call the function sum and display the sum of these two numbers. What is the best way to display the return value?



My code so far:



<form>
<input type=number id=firstnumber/>
<input type=number id=secondnumber/>
<input type=button onclick=summa(document.getElementById(firstnumber),document.getElementById(secondnumber)) id=Summa value=Calculate sum/>
</form>
<div>
<script type=text/javascript>
document.write(document.getElementById(Summa))
</script>
</div>


The function:



<script type=text/javascript>
function summa(number1, number2)
{
var sum = number1 + number2;
return sum;
}
</script>

More From » html

 Answers
1

There are a couple of things a little wonky about what you're doing. Firstly, you aren't passing the values from the inputs to your function; you're passing the elements themselves. Secondly, in the attempt to write the value to the document you are again referencing an element and not the element's value. Lastly, you would be attempting to add together the elements (or their string values, if you were retrieving them), which would only concatenate together into a new string instead of performing the actual math.



This is my approach to really cleaning up and fixing your attempt:



First, let's get rid of the ugly and obtrusive onclick event handler altogether (we'll wire up the event handler differently later):



<input type=button id=Summa value=Calculate sum/>


Add a third input so we can display the sum:



<input type=text id=sumnumber />


And now let's do all the JavaScript work in one place, as the last thing before your closing </body> tag. This keeps the HTML clean and not littered with inline JavaScript, so you don't have to dig through it when you need to make changes.



    ...
<script type=text/javascript>

// Your new summing function definition that takes input values
function summa(valueOne, valueTwo) {
// Convert the strings to numbers before doing the math
return Number(valueOne) + Number(valueTwo);
}

// Now let's hold onto some references to the elements in the page
var sumButton = document.getElementById('Summa');
var firstNumber = document.getElementById('firstnumber');
var secondNumber = document.getElementById('secondnumber');
var sumNumber = document.getElementById('sumnumber');

// Wire up the event
sumButton.onclick = function() {
// Re-use the variables outside of this scope to retrieve
var sum = summa(firstNumber.value, secondNumber.value);
// Update the sum element with the result of the function
sumNumber.value = sum.toString();
};

</script>
</body>


Here's a working jsFiddle Demo.



Note: If this were going to be production code and not just a teaching example, there are other things I would do, including: (1) number validation, and (2) lock down the inputs (maxlength, etc.) and outputs (update to a <span> or similar), among other minor things.


[#46748] Tuesday, March 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;