How to addition two input txt

I creat a form with 3 #input number but i would like to know how to additionnal in #input3
#input1 + #input2 ?

Thank you

Please need help…

Hi kemel,

please share more context about the problem you are trying to solve.
common use case is to have a 3rd text, not input that will sum the 2 other inputs, when one of those changes in the form, without saving them in the db
another example is a calculated field that you might want to save in the db just before the save, or not save in the db but calculate just after the read. for instance currency exchange rate, tax etc
for that please refer to db hooks

good luck,
Shlomi

Hello Shlomi, thank for your help,
In the form i have 3 input and one button for submit to the data the 3 input

Input1 and input2 its for whriting number only default 0
and input3 its the sum Input1 and input2

i need the code java
if you want i can built a site for that

thanks you
kemel

hi,

please share your wix code site with the issue you described, ill have a look

Shlomi

kemelhaidar.wixsite.com/monsite

I have a similiar question. My code is below. If it is terrible, I apologize. I am very new to this. I also tried parseFloat for what it is worth. I don’t actually know their distinctive use cases, but it did not help me. Console shoots out the two inputs, but not doing the math. Input 1 = 10 and Input 2 = 20 it outputs 1020, not 30. I appreciate the help!

export function submitCalc_click(event, $w){
var a = String(parseInt($w(“#input1”).value));
var b = String(parseInt($w(“#input2”).value));

var n = a + b; 

	console.log(n); 
	
	}

Hi Karson,

when ‘adding’ to strings, Javascript code will concatenate them, so ‘10’ + ‘20’ will become ‘1020’
what you want to do is concatenate to integers 10 + 20 => 30

please have a look here: (btw, this is plain old javascript, not wix related :slight_smile: )

export function submitCalc_click(event, $w){
	var a = parseInt($w("#input1").value);
	var b = parseInt($w("#input2").value);
	var n = a + b;
	console.log(n);
}

good luck,
Shlomi

Hi Kemel,

please use this code below, for each of the inputs 1-5, call the recalc function on the input ‘onchange’

export function recalc6(event, $w) {
	var sum = 0
	if($w('#input1').value)
		sum += parseInt($w('#input1').value);
	
	if($w('#input2').value)
		sum += parseInt($w('#input2').value);
		
	if($w('#input3').value)
		sum += parseInt($w('#input3').value);
	
	if($w('#input4').value)
		sum += parseInt($w('#input4').value)
		
	if($w('#input5').value)
		sum += parseInt($w('#input5').value)
	
	$w("#input6").value =  sum;
}

export function recalc7(event, $w) {
	var sum = 0
	if($w('#input1').value)
		sum += 5*parseInt($w('#input1').value);
	
	if($w('#input2').value)
		sum += 10*parseInt($w('#input2').value);
		
	if($w('#input3').value)
		sum += 20*parseInt($w('#input3').value);
	
	if($w('#input4').value)
		sum += 50*parseInt($w('#input4').value)
		
	if($w('#input5').value)
		sum += 100*parseInt($w('#input5').value)
	
	$w("#input7").value =  sum;
}

export function recalc(event, $w) {
	recalc6(event,$w);
	recalc7(event,$w);
}

Thanks!!!

But one problem the Input 6 et 7 don’t go to the database after i submit

Hi kemel,

right this is out of the scope of the original question you have asked, you will need to implement an onclick event on the page button to use wix data insert of the values to the db

Shlomi

And how i will use insert? Sorry but i m very novice…

Hi kemel,

in the previous answer i have attached a link to the reference documentation with example how to do it. did you read it? this scenario is a bit tricky and assumes you have some coding experience or the ability to learn from code samples

Shlomi

Hi Shlomi,
Yes i saw the link, i m trie but i dont where to put the code in to your code and how sorry. I will trie again but i need the solution before wednesday… anyway i will use a calculator app
Thanks you for your help
Kemel

I’m triying this but is not working

Hi Kemel,

please share the code you are using and let me know if you have any question you can use help with

Shlomi

Have make this code after your code but is not working

all input go to the data with dataset only 6 ans 7 don’t want


I have to line red

export function button1_click(event, $w) {
let toInsert = {
“Number”: $w(“#input6”).value ,
“Number”: $w(“#input7”).value ,

};
$w.wixData.insert(“Base”, toInsert)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errorMsg = err;
} );

}

Shlomi any idea why i have the red line in dataset?

Hi Kemel,

we need to use the following code:

export function recalc6(event, $w) {
	var sum = 0
	if($w('#input1').value)
		sum += parseInt($w('#input1').value);
	
	if($w('#input2').value)
		sum += parseInt($w('#input2').value);
		
	if($w('#input3').value)
		sum += parseInt($w('#input3').value);
	
	if($w('#input4').value)
		sum += parseInt($w('#input4').value)
		
	if($w('#input5').value)
		sum += parseInt($w('#input5').value)
	
	$w("#input6").value =  sum;
	$w("#dataset1").setFieldValue("billetTotal", sum);
}

export function recalc7(event, $w) {
	var sum = 0
	if($w('#input1').value)
		sum += 5*parseInt($w('#input1').value);
	
	if($w('#input2').value)
		sum += 10*parseInt($w('#input2').value);
		
	if($w('#input3').value)
		sum += 20*parseInt($w('#input3').value);
	
	if($w('#input4').value)
		sum += 50*parseInt($w('#input4').value);
		
	if($w('#input5').value)
		sum += 100*parseInt($w('#input5').value);
	
	$w("#input7").value = sum;
	$w("#dataset1").setFieldValue("amount", sum);
}

export function recalc(event, $w) {
	recalc6(event,$w);
	recalc7(event,$w);
}

Shlomi