Simple calculation based on user input

I am trying to display an output form with the answer to a calculation (multiplying the number by 2) based on a number the user puts. For example, if the user types 4, the output box displays 8. I tried to do an alert and it did not work. Ultimately I am not going to do these simple calculations, but if I know how to do this then I’m good. Thank you.

like this…

$w.onReady( function () {

let mysteryNumber = $w(‘#input1’).value; //input1 is the id of your input, change as necessary

let productNumber = mysteryNumber*2;

console.log(productNumber); 

$w('#outputBox1').value = productNumber;    //outputBox1 is the id of your box, change as necessary  

})

I am trying this and it is not working. I want the final payment amount (#input10) to be 1.0325 X Invoice amount (which the user enters #input11).

Any help would be much appreciated. Seems like this should be simple.

Here is my code based on the above:

$w.onReady( function () {
let invoiceAmt = $w( ‘#input11’ ).value; //input1 is the id of your input, change as necessary

let paymentAmt = invoiceAmt* 1.0325 ;

console.log(paymentAmt); 

$w( '#input10' ).value = paymentAmt;     //outputBox1 is the id of your box, change as necessary  

})

The velo editor is showing me these errors:


and

You can see the form at tristatewastesolutions . com / pay-your-bill

Did you set the input-field to Text or Number?

Edit: If its text →

i tried it for research & learning

export function textBox1_change(event) {
let randomvalue = parseInt($w( ‘#textBox1’ ).value); // convert the text to a number
var multipliedvalue = randomvalue* 2 ; //multiply that number
var outputvalue = multipliedvalue.toString(); // convert the number to text
console.log(outputvalue); // Shows the final value
$w( “#text21” ).text = outputvalue; // set the text
}

this works for me, i added a change event so it updates the textbox

Thank you, I will try this out, and I will also try changing the input to number. I should have thought of that.

Again… thank you so much for responding.

@naturfellparadies
Seems like this is really close, but it doesn’t like the text property.

The field I am trying to drop the number into is the payment amount field in a Wix Payment Form.

export function input11_change(event) {
let randomvalue = parseInt($w( ‘#input11’ ).value); // convert the text to a number
var multipliedvalue = randomvalue* 2 ; //multiply that number
var outputvalue = multipliedvalue.toString(); // convert the number to text
console.log(outputvalue); // Shows the final value
$w( “#input10” ).text = outputvalue; // set the text
}

@mic You cannot use .text on an input element. You can use .value .placeholder etc.

You can only use .text on a text element.

@code-queen
Thanks! I tried both value and placeholder and neither works. When I enter a number in #input12 I want to perform a calculation (in this case, multiple by 2) and then drop that number into #input12. Just can’t get anything to work. Seems like it should be simple enough.

export function input11_change(event) {
let randomvalue = parseInt($w( ‘#input11’ ).value); // convert the text to a number
var multipliedvalue = randomvalue* 2 ; //multiply that number
var outputvalue = multipliedvalue.toString(); // convert the number to text
console.log(outputvalue); // Shows the final value
$w( “#input12” ).value = outputvalue; // set the text
}

@mic hey,
you have to take care what you’re putting where.

the parseInt only works if you get a text, if you changed your input into number you can directly put in $w(“#element”).value so if you want to set the output as text element you use .text but if you want to put it out in a textinput(Dont know why you would want that only if you want to calculate it into both directions). you need to put it into .value

i’ve seen you website and i think you should should make a textelement instead of a textinput as output.

as example “Put here your Invoice Amount”
and next to it, the user will see a textelement with “Your final amount to pay” and the value next to it.

i hope its understandable

also post your whole code so we can help you better.

$w.onReady( function () {
$w( ‘#input1’ ).onChange((event)=>{
let value = event.target.value;
let a = value * 2 ;
$w( ‘#input2’ ).value = a+ “” ;
})
});

@mic Hello, This code will work. set both inputs type to a number. So, whenever the number changes in #input1 then #input2 number will also change to #input1 *2.

Simple calculation could be really simple when using → RETURN-FUNCTIONS.

Take a look here…
https://russian-dima.wixsite.com/meinewebsite/simple-return-function

This worked!!! Thank you very, very much!

Many thanks! That worked for me, too!
But when I try to add up two input fields I get an error that b can not be found. Can anyone help me with this issue?

Your b variable Need to be a global variable. The error is because you have defined b variable in one function and using in another function. So, define your global variables above onReady() function.

Hi @justt103
I am unfortunately a total beginner in coding.
I have now defined the global variables. However, I do not know how to make the code run so that the input1 + input2 is added no matter which input changes.
e.g. If I insert a 4 in Input1, Input3 should show a 4, because nothing has been entered in Input2 yet. If I enter a 5 in Input2, for example, Input3 should calculate 4+5=9.
Somehow I can’t get this to work.
Sorry, I hope that I do not annoy anyone here with such beginner problems :slight_smile:

@artur-hubert87

  1. Tip-1: Do not use pics to show your code. Why?
    Because it is waste of time for the helpers, to retype all your code from a pic.

  2. Tip-2: Do not bump-up old posts. Better you open always your own one. Why?
    Because this way, you will probably get much faster the answer you are searching for.
    -The post is more clear and not overloaded with any additional or useless stuff and is concentrated only onto your topic.

  3. Back to your QUESTION and SOLUTION: How to solve your problem ?

As already mentioned above, you have the option to create GLOBAL-VARIABLES.

What are GLOBAL-VARIABLES?

Global variables are variables, which can be used from every position of/in your code.
These variables are not dependent to a certain function or code-part. You can use them in every part in your code, where ever you want to.

POSSIBLE-SOLUTION-EXAMPLE:

var a,b;

$w.onReady(function () {
    $w('#input1').onChange((event)=>{a = Number(event.target.value); CALCULATION();});
    $w('#input2').onChange((event)=>{b = Number(event.target.value); CALCULATION();});
});

function CALCULATION() {console.log("Calculation running..."); console.log(a+b);}

Here you can see, a function called CALCULATION, which do all the magic.
Variable-a and variable-b are declared/defined globaly.

Not tested, but should normaly work for you.

You will find the results in :arrow_right: CONSOLE.

Of course there are also other possible solutions for this issue.

Hi @russian-dima Many thanks for your advice and the code! It works perfectly :blush: