Set Kendo Switch checked via javascript

1 Answer 93 Views
Switch
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Giovanni asked on 31 May 2022, 08:37 AM

Hi, I would like to use a kendo switch inside a form: I' created a little example at:

https://stackblitz.com/edit/react-k3fw3s

 

I need to set the initial value programmatically.

So, I created a function switchSetOn (in the file MyUtils.js) to do this using jquery.

This function works well, because the switch correctly appears as ON, but if I click on it the first click does not work and the control remains ON (after a small movement): from subsequent clicks it works fine.

So, I think that the react control keep is value to false even if I set (via jquery) the aspect to true.

Is there a way to set the value correctly via jquery? (I don't want to use defaultChecked or checked property of the control)

Another problem is that, when I click on the submit button the value of the hidden input "checkbox" of the switch does not fall into the json (I can see only the value of the input "text"). Is there something I have to do to obtain the value?

Thank you

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 02 Jun 2022, 06:57 AM

Hi Giovanni,

If the requirement is to set initial value to the Switch component you can use the "defaultValue" and "defaultChecked" properties:

As for programmatically setting the value, you can get reference to the component and use its "toggle" function. I have modified the example, so it can use the toggle function instead:

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
commented on 06 Jun 2022, 07:31 AM

Thank you, the "toggle" function works fine for my needs.

But, is this toggle function a kendo function? And where is documented?

Konstantin Dikov
Telerik team
commented on 08 Jun 2022, 07:01 AM

Hi Giovanni,

The toggle function is not mentioned in the API of the Switch and it is used by the component for its internal functionality. However, it is useful and I will talk with the team to see if we can include it in the documentation.

 

Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
commented on 09 Jun 2022, 07:33 AM

Ok, thank you.

Tags
Switch
Asked by
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Share this question
or