Loop through all switches and get checked state.

1 Answer 23 Views
Switch
Patrick
Top achievements
Rank 1
Iron
Patrick asked on 23 Sep 2021, 09:16 PM

I have multiple switches on a page, each switch has the same class assigned to it.

What i need is that if any switch is checked / changed i need an event to fire and also need to check the state of the remaining switches.

@(Html.Kendo()
    .SwitchFor(a => a.IsAdmin)
    .HtmlAttributes(new { @class = ".permissions" })
    .Messages(c => c.Checked("YES").Unchecked("NO")))

I have tried numerous jquery approaches but none of them even fire when the switch is toggled.


    $('body').on('change', '.permissions', function () {
        // this event handler never gets trigged.....
        // get an instance of each switch and get the toggled / checked state.
        alert();
    });

1 Answer, 1 is accepted

Sort by
0
Patrick
Top achievements
Rank 1
Iron
answered on 24 Sep 2021, 07:36 AM

Oh man.

I named my class incorrectly in my switch.

the final code looks as follows


@(Html.Kendo()
    .SwitchFor(a => a.IsAdmin)
    .HtmlAttributes(new { @class = "permissions" })
    .Messages(c => c.Checked("YES").Unchecked("NO")))


$('body').on('click', '.permissions', function () {
   alert();
});

Ivan Danchev
Telerik team
commented on 28 Sep 2021, 12:43 PM

Hello Patrick,

Alternatively, you can attach a Change event handler in the declaration of the Switch:

@(Html.Kendo()
	.SwitchFor(a => a.IsAdmin)
	.HtmlAttributes(new { @class = ".permissions" })
	.Messages(c => c.Checked("YES").Unchecked("NO"))
	.Events(ev => ev.Change("onSwitchChange"))
)

<script>
	function onSwitchChange(e) {
		//get a reference to the Switch that fired the event:
		var widget = e.sender;

		alert("change");
	}
</script>

Tags
Switch
Asked by
Patrick
Top achievements
Rank 1
Iron
Answers by
Patrick
Top achievements
Rank 1
Iron
Share this question
or