I am working on asp.net MVC 5. I have placed a bootstrap toggle switch button having input type checkbox
Bellow is the image of my toggle switch
From my query i am getting a command name cmd_Name
in which there is On or Off value in it and based of this value i just want to change the toggle switch from On-Off or from Off-On
Bellow is my razor syntax for toggle
@using (Html.BeginForm())
{
//var cmd_Name = Session["cmd_Name"];
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset style="height:60px">
<legend style="text-align:center; font-size:large; font-family:'Times New Roman'; background-color:#C8E6C9; color:red">Remote On/Off</legend>
<input id="test_id" name="cmdName" type="checkbox" checked data-toggle="toggle">
</fieldset>}
Bellow is my script for toggle
<script type="text/javascript">
var search = '@Session["search"]';
var cmd_Name = '@Session["cmd_Name"]';
var data = {}
//alert(cmd_Name);
$(window).on('load', function () {
// here cmd_Name i having On or Off value
// i want to put a check like bellow
if(cmd_Name == "On")
{
// then the toggle switch (checkbox) moves to On
// what should place here that moves the switch from on to off and vise versa
}
else
{
//then toggle switch (checkbox) moves to Off
}
})</script>
Updated Code
Bellow is image for my updated code and error
Updated Code 2
I have following declarations in my head
section in layout
<head>
<title>@ViewBag.Title</title>
@*All the javascript and css files that are required by the
application can be referenced here so that there is no
need to refrence them in each and every view*@
<script type="text/javascript" src="~/Scripts/jquery-3.1.0.min.js"></script>
<script src="~/Scripts/bootstrap-toggle.js"></script>
<link href="~/Content/bootstrap-toggle.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
<script type="text/javascript" src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
<script src="http://maps.google.com/maps/api/js?key=MyKey"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="favicon.ico" type="image/ico" />
<link rel="shortcut icon" href="~/favicon.ico" /></head>
See bundle.config
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
I have tried many things in if...else
like $("#test_id").checked = true/false
also i have tried document.getElementById("test_id").checked
But all in vain, any help would be appreciated