Cannot Reference Kendo Switch

2 posts, 0 answers
  1. Mark
    Mark avatar
    12 posts
    Member since:
    Jun 2018

    Posted 02 May Link to this post

    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??????

     

     

     

  2. Preslav
    Admin
    Preslav avatar
    516 posts

    Posted 06 May Link to this post

    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.
Back to Top