I am trying to do basic math with java and Wix Code. I need to use the input from users to the website. I added user input boxes and when the user clicks a button, I am wanting to add those inputs together and then show the result in a separate box or ouput. Here is my code…that does not work. How can I fix this?
export function button1_click(event, $w) {
function add_number() {
var a = ($w.document.getElementById($w(‘#input1’)).value);
var b = ($w.document.getElementById($w(‘#input5’)).value);
var result = a + b;
$w.document.getElementById($w('#input18')).value = result;
}
}
Hi,
Input elements in the web world are returning strings, so you need to convert the string to a number before you do math calculations. You can use th javascript parseInt function for that.
I used 14 “user input” fields and needed to add them all together. I changed the names of the user input boxes and used the $w(#“…”) references for them. Below is the code that actually worked for me. I then had added a text box, in the example below referenced as $w(“#text24”), that is where the mathematical output was displayed.
$w.onReady( function () {
});
export function calcTotalButton_click(event, $w) {
let num1 = parseFloat($w(“#taxes”).value)
let num2 = parseFloat($w(“#insurance”).value)
let num3 = parseFloat($w(“#hoa”).value)
let num4 = parseFloat($w(“#prop”).value)
let num5 = parseFloat($w(“#cable”).value)
let num6 = parseFloat($w(“#utilities”).value)
let num7 = parseFloat($w(“#repairs”).value)
let num8 = parseFloat($w(“#netflix”).value)
let num9 = parseFloat($w(“#cleaning”).value)
let num10 = parseFloat($w(“#lawn”).value)
let num11 = parseFloat($w(“#snow”).value)
let num12 = parseFloat($w(“#club”).value)
let num13 = parseFloat($w(“#misc”).value)
let num14 = parseFloat($w(“#pmi”).value)
let calc = (num1) + (num2) + (num3) + (num4) + (num5) + (num6) + (num7) + (num8) + (num9) + (num10) + (num11) + (num12) + (num13) + (num14)
let num15 = $w(“#text24”)
num15.text = String ( calc )
}
1 Like
Some reason I can not get this to work…
export function button_click(event, $w) {
let num1 = parseFloat($w(“#one”).value)
let num2 = parseFloat($w(“#two”).value)
let calc = (num1) + (num2)
let num15 = $w(“#answer”)
num15.text = String ( calc )
}