# Wix Code, basic math, and Java 

**URL:** <https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [August 19, 2018, 4:58pm UTC](https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427 "2018-08-19T16:58:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian\_bishop](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@brian\_bishop](https://forum.wixstudio.com/u/brian_bishop)\
**Post date:** [August 19, 2018, 4:58pm UTC](https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427/1 "2018-08-19T16:58:39Z")

</div>

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; 

        }  

```

}

---

<div class="post-metadata">

**Author:** ![tomer-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/tomer-wix/32/676_2.png) [@tomer-wix](https://forum.wixstudio.com/u/tomer-wix)\
**Post date:** [August 20, 2018, 10:34am UTC](https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427/2 "2018-08-20T10:34:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brian\_bishop](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@brian\_bishop](https://forum.wixstudio.com/u/brian_bishop)\
**Post date:** [August 25, 2018, 10:32pm UTC](https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427/3 "2018-08-25T22:32:22Z")

</div>

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 )  
}

---

<div class="post-metadata">

**Author:** ![jgioffre](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jgioffre](https://forum.wixstudio.com/u/jgioffre)\
**Post date:** [January 29, 2020, 5:18am UTC](https://forum.wixstudio.com/t/wix-code-basic-math-and-java/1427/4 "2020-01-29T05:18:19Z")

</div>

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 )  
}
