This is a migrated thread and some comments may be shown as answers.

Cannot Reference Kendo Switch

1 Answer 498 Views
Switch
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 02 May 2019, 06:14 PM

I have a two Switch widgets on my page and have a change event setup for one of them. In the change function i'm trying to get a reference to the other switch on the page to see if it is checked. All I get is 'Undefined'.

<div class="col-md-6">
        <div class="form-group pt-2">
            @(Html.Kendo().Switch()
                  .Name("Immediate")
                  .Messages(c => c.Checked("YES").Unchecked("NO"))
                  )
            <label for="reportable">Save changes immediately</label>
        </div>
        <div class="form-group">
            @(Html.Kendo().Switch()
                  .Name("Scheduled")
                  .Messages(c => c.Checked("YES").Unchecked("NO"))
                  .Events(e => e.Change("onScheduledChange"))
                  )
            <label for="history">Schedule/Back-Date Changes</label>
        </div>
    </div>

and here's my JS:

<script>
    function onScheduledChange(e) {
        var immediateSwitch = $("#Immediate").checked;
        if (e.checked) {
            //alert("scheduled is checked");
            alert(immediateSwitch);
        }
    }
</script>

immediateSwitch returns undefined.

I've also tried this:

<script>
    function onScheduledChange(e) {
        var immediateSwitch = $("#Immediate").data("kendoSwitch");
        if (e.checked) {
            //alert("scheduled is checked");
            alert(immediateSwitch.checked);
        }
    }
</script>

 

and got the same results. How do you get a reference to another Switch on the page??????

 

 

 

1 Answer, 1 is accepted

Sort by
0
Preslav
Telerik team
answered on 06 May 2019, 08:15 AM
Hello Mark,

The correct code of getting the Switch reference is the second one:

var immediateSwitch = $("#Immediate").data("kendoSwitch");

However, to get its value, use the value method:

immediateSwitch.value()

Please, give this a try, and let me know if it works for you.


Regards,
Preslav
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Anoop
Top achievements
Rank 1
commented on 21 Feb 2024, 10:06 AM

What change should I make to get the below error?

My kendo switch has Deferred loading.

immediateSwitch.value() is not defined

Ivan Danchev
Telerik team
commented on 23 Feb 2024, 06:51 AM

Hello Anoop,

Add your script that gets a reference to the Switch after the DeferredScripts method call. You can also call the logic in a document.ready handler, e.g.,

 @Html.Kendo().DeferredScripts()

<script>
    $(document).ready(function() {
         var immediateSwitch = $("#Immediate").data("kendoSwitch");
         var value = immediateSwitch.value();
         console.log(value);
    })
</script>

Additionally, make sure you are using the actual Name of your Switch at the place highlighted above. 

Tags
Switch
Asked by
Mark
Top achievements
Rank 1
Answers by
Preslav
Telerik team
Share this question
or