Skip to content Skip to sidebar Skip to footer

How To Change State Of A Switch Dynamically Using Jquery?

I have a switch icon purely designed in CSS. I got the design for switch from this page https://www.w3schools.com/howto/howto_css_switch.asp and some added modifications on the swi

Solution 1:

Actually, I was just doing this last night on the same slider that you are.

the way to do it is

checked = ...; //boolean
$(slider).children("input").prop("checked",checked);

To check slider on/off, use

$(slider).children("input").is(":checked");

Solution 2:

You have a hidden checkbox which toggles the switch. So doing this will toggle it on:

$('#togBtn').prop('checked', true);

and off:

$('#togBtn').prop('checked', false);

EDIT: Added full code

So your full code would be:

if (response.data == "t") {
    $('#togBtn').prop('checked', true);
}
elseif (response.data == "f") {
    $('#togBtn').prop('checked', false);
}

Post a Comment for "How To Change State Of A Switch Dynamically Using Jquery?"