Page 1 of 1

two inputs, one label for output (calculation)

Posted: Tue Feb 04, 2014 8:19 pm
by Michael P. Eckhardt

I have two input fields for values x and y, one button and a label, which should show the value of x+y after pressing the button.

I am able on pushing the button to get the values of both input fields and store them in appropriate variables.
Also I can change the text in the label according to these two values (x and y).

But I do not know where I need to declare the variable z, assign the value of z=x+y and use the value of z to output it into my label.

Who can help me out (or point me where I can find the appropriate info?)?

Thanks!


two inputs, one label for output (calculation)

Posted: Tue Feb 04, 2014 9:05 pm
by Maryna Brodina

Hello! It can be done with custom JS on button click. On button click add JS where you retrieve input values, sum them and set result in Label.


two inputs, one label for output (calculation)

Posted: Tue Feb 04, 2014 9:16 pm
by Michael P. Eckhardt

Hi Maryna,

thanks for the feedback.

Of course this is done via JS, but where do I enter the relevant coding?
In the tutorial I can only find a section, where I can enter my name and have an output like ("Hello "+name). Where and how would I enter a function to calculate and change label value?

So far I have 4 events on the button.
1 each for retrieving the inputs values.
1 for calculating the sum
1 for changing the label's value

I think the last one can be omitted.

But the calculation of the sum goes somehow like this:

function calcsum() {
var z = x+y;
document.setData("lbl_output", z);
}

Still the label is not updated.

What am I missing?

Thanks.
Michael


two inputs, one label for output (calculation)

Posted: Tue Feb 04, 2014 10:22 pm
by Maryna Brodina

Delete all actions, you need only Run JavaScript action on button click with the following code: prevar n1 = parseFloat(Appery("input1Name").val()), n2 = parseFloat(Appery("input2Name").val());
Appery("labelName").text(n1+n2); /prewhere input1Name, input2Name, labelName - component names on page.


two inputs, one label for output (calculation)

Posted: Tue Feb 04, 2014 10:57 pm
by Michael P. Eckhardt

Thanks!

This did work.

Cheers
Michael


two inputs, one label for output (calculation)

Posted: Thu Mar 13, 2014 7:46 pm
by iPsyc Videos

I tried Mayna's code, but it's not calculating my simple calculator total. I have 2 inputs (number1, number2). and a button to run javascript on click (copied from her post). the labelName is "lablemytotal" and is a Lable. Any suggestions?

Hunter


two inputs, one label for output (calculation)

Posted: Thu Mar 13, 2014 9:28 pm
by Nikita

Hello,

Are there any errors in console?


two inputs, one label for output (calculation)

Posted: Fri Mar 14, 2014 12:51 am
by iPsyc Videos

Uncaught SyntaxError: Unexpected identifier /app/view/cd98d2b1-88bf-4382-b52c-c640f2aceb25/startScreen.js:77
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery-1.9.1.js:3345


two inputs, one label for output (calculation)

Posted: Fri Mar 14, 2014 12:59 am
by iPsyc Videos

never mind, had some junk code I forgot about on page load. thanks


two inputs, one label for output (calculation)

Posted: Fri Mar 14, 2014 1:00 am
by iPsyc Videos

I did not know about the console, so thanks NIkita!