#unable to add code to the button and making it functionable

1 messages · Page 1 of 1 (latest)

gentle solstice
#

I want the button to return a value for which it takes two numbers as input from two label widgets,named input1 and input2 as shown in the image attached and upon clicking on button named 'Calculate', it should perform a calculation mentioned in the code and return the value in a text editor named as 'output'. But, i'm unable to do so, it shows some or the other errors. Please help me out, how do I fix this? Please, refer to the image attached which displays the code as well as the widget names.

red fogBOT
#

Hey There,

We've registered your query, and our team will get back to you soon.

Regards,
Pranav

red fogBOT
#

Hello! Please move your code to a function inside a JS Object:
https://docs.appsmith.com/core-concepts/writing-code/javascript-editor-beta#docusaurus_skipToContent_fallback
Then, when setting the output, please use the setText() method of the Text
widget, because direct assignments like the ones used in your code will not work
in Appsmith:
https://docs.appsmith.com/reference/widgets/text#settext-param-string-promise
[https://docs.appsmith.com/reference/widgets/text#settext-param-string-promise]

Please let us know if you need more help or face issues.

Thank you for your ongoing support and patience. We aim to provide you with
helpful assistance and hope we have addressed your question adequately.

Please take a moment to share your experience with our support team.
https://survey.frontapp.com/v2/09a400bf433bc9676d67/d88df259c5dcb320b61d9ace48cd03f9
[https://survey.frontapp.com/v2/09a400bf433bc9676d67/d88df259c5dcb320b61d9ace48cd03f9]

The JavaScript Editor in Appsmith enables you to create JS Objects with a page-level scope. A JS Object is an encapsulation of variables and functions associated with it. It is a template similar to a Java class that contains the variables and the methods used to perform actions.

Learn how to use the Text widget to display static or dynamic textual information

gentle solstice
#

I am still facing the error. To give you the context of what I am doing is:
I need to put a code to a button widget, named 'calculate', upon clicking the button it should perform the following calculations which needs to be coded. So first there are two input widgets, which would take two numbers as inputs, first input widget name is 'CalcInputforEnergyDistrib1' and its label name is 'input1', the second input widget name is 'CalcInputforEnergyDistrib2' and label name of 2nd widget is 'input2', upon entering the values in respective widgets, and on clicking the calculate button, the calculate button widget name is 'Button1' and label name is 'Calculate', it should perform the calculations and return the result on a text widget , the text widget name is 'Text1' and text name is 'output'. Below is the image attached of the widgets I mentioned.
Also, I have attached the image which shows the code I have pasted over the JS object editor and the image also shows the error I get after I run. Please, help me to resolve my query as fast as possible.

red fogBOT
#

Hello there!
On line 9, I see that you declared an outputWidget variable as
this.myVar2.Text1. You then further went on to call .setText() on it.
I assume you're trying to call setText() on the text widget itself. Am I
correct?

#

If that's the case, you can just refer to your output widget as:

WidgetName.setText()

gentle solstice
#

Thanks for your resposne. I did whatever you have told, I have attached the picture of the modified code. After I ran the code, it again throws an error like:TypeError: Cannot read properties of undefined (reading 'text') , I have attached the pic below of the modified code and error.

Also, please guide me in the calculate button, what do I specify in the onClick JS element, for which I have attached a pic and given an arrow mark for that. Please, help me out and give you response as fast as possible.

gentle solstice
#

Please, provide any response. I am unable to configure that. I would be really kind and grateful if you could help me out!!