Adding two "Input" values

I have created two input cells on a page called “square2V” and “teamA”. Both are set to be numerical values.
When I multiply the two cells using the code below, it successfully multiplies the values and labels a button called “teamAScore” with this value.

$w("#teamAScore").label = ($w("#square2V").value * $w("#teamA").value).toString(); 

However, when I try to add the two cells by changing the * to a +, it adds them as text.
eg. 30 + 40 = 3040

Is it possible to add the two values as numbers.
Thanks

hey
you have to make the script think of them as numbers not strings.

+num1 + +num2;

adding the unary + before is a quick solution.

read more here
http://xkr.us/articles/javascript/unary-add/

Hey,
You can also use the function parseInt before adding them.

Good luck,
Tal.

Thanks both! The +num1 + +num2 method worked!

1 Like

Hello
can you help me for the code to addition two input in the third input on my form

#input3 = #input1 + #input2
20 = 10 + 10

All input is number

thanks you
kemel

Hi kemel,

can you share the url of the site you are building?

thanks,
Shlomi

Hi All

We would appreciate much if you can share us the whole code here. I been trying to do simple math but cant figure it out.

Thanks,
DA

Hi guys.
I have a quoteDate field in the collection.
I want to add a number of days to that date to show ad deadlineDate.

How do I add numbers to a date value in the data.js file?

Here it what I use:

$w.onReady( function () {

$w(‘#Calculate1’).onClick( function () {

console.log(“button has been pressed”)

$w(‘#totalmonths1’).value = $w(‘#convertyrs1’).value + $w(‘#Month1’).value

console.log(“calculation finished”)

}) 

})

it works but only combines the values instead of add the values. example: input1 = 12 input2 = 1
the above code returns the value 121 instead of 13 (12+1)

above vaguely referenced:

+num1 + +num2;
adding the unary + before is a quick solution.

or

use the function parseInt before adding them.

But it is not enough information to understand what changes need to be made.

I believe I need a code that will recognize the values as a number instead of a string but i am not certain .

Change this

$w(’ #totalmonths1 ‘).value = $w(’ #convertyrs1 ‘).value + $w(’ #Month1 ').value

To this

Let first = $w(’ #convertyrs1 ‘).value;
Let second = $w(’ #Month1 ‘).value;
$w(’ #totalmonths1 ').value = first+second

i am getting a “parsing” error for the words first and second. Not sure why or how to fix it.

Lowercase, Change Let to let

Android phone playing havok

Thanks for helping me get through this Mike!

i used:

$w(‘#totalmonths1’).value = “” + (Number($w(‘#Years1’).value) *12 + Number($w(‘#Month1’).value));

and it works so far! it has added the two user input values successfully

Hi Andreas,
hey could you take a look at my forum post:

maybe you can point me in rthright direction. thank you kindly.e

is this the same logic you would user for a “likes” counter which counts the number of likes?

@shaneicewilliams @mikemoynihan99 Hello, thank you so much for the calculation code! I was able to get my calculations to work properly thanks to both of you, but now how do you get the calculated value inserted into the database? I have tried to connect the element input to the database, and have tried to add a submit button, but the collection field is still empty. The calculations shows up on the page inside the correct element input box “fbTotal” , but even though its linked to the collection, it doesn’t export or show up. I need that number in there so I can display that total on a different page on my website. Please help and thank you!

Here’s my code:

$w.onReady( function () {

$w(‘#Calculate1’).onClick( function () {
console.log(“button has been pressed”)
$w(‘# fbTotal ‘).value = “” + (Number($w(’#dropdown1’).value) + Number($w(‘#dropdown2’).value) + Number($w(‘#dropdown3’).value) + Number($w(‘#dropdown4’).value) + Number($w(‘#dropdown5’).value) + Number($w(‘#dropdown6’).value) + Number($w(‘#dropdown7’).value));

console.log(“calculation finished”)
})
})