Summed fields not displaying properly

Trying to add two fields and then have it automatically displayed in a new textbox; used the following codes:

$w.onReady( function () {

var num1 = Number(‘#text20’)
var num2 = Number(‘#text21’)

var numtotal = (+num1) + (+num2)

var text22 = numtotal

});

But text 22 does not display the sum (only gives 0 [zero]). What am I doing wrong?

Many thanks but desperately need help!

1 Like

Hi Kenny,

First problem is the declaration of your text box, you should take the value of that text box also round it with Number, so it should look something like that :

Number($w("#text20").value);

second, your textbox 22 looks like a variable, it should be an element so that you can show the results.
Also, to be aware of, you put your code in the onReady function without any button, so it will only work once ( when the page ready )

Here is a snippet code to make it easier to see how it works:

  • Note : we have 2 text boxes and a Text that shows the result.
let x = Number($w("#text20").value) + Number($w('#text21').value);

    $w("#total").text = x.toString();

Hope this helps!
Best.

Mustafa