html - Toggle a Bootstrap Toggle element with JavaScript (and show animation) -
i have bootstrap toggle element follows:
<input id="neutralind" type="checkbox" name="neutralsite" onchange="seteventsite('neutral')" data-toggle="toggle" data-on="yes" data-off="no">
upon clicking it, toggle between yes , no, , executes javascript function other stuff (code below).
here's want do:
upon selecting radio button elsewhere on page, execute javascript toggles bootstrap toggle no. here code have far:
defining radio buttons:
<div class="radio"> <label><input type="radio" id="hometeam<?php echo $row; ?>" name="hometeam" onchange="seteventsite('team')"></label> </div>
javascript function code:
function seteventsite(chosen) { var neutralind = document.getelementbyid("neutralind"); // toggle var locationoverride = document.getelementbyid("locationoverride"); // text field var hometeam = document.getelementsbyname("hometeam"); // radio buttons if (chosen == "neutral") { // toggle clicked (changed) if (neutralind.checked) { locationoverride.readonly = false; for(var i=0; i<hometeam.length; i++) { hometeam[i].checked = false; } } else { locationoverride.value = ""; locationoverride.readonly = true; } } else if (chosen == "team") // radio button selected { neutralind.checked = false; neutralind.value="off"; locationoverride.value = ""; locationoverride.readonly = true; } }
when choose radio button, sets text field readonly, bootstrap toggle not appear toggle (it remains set yes when should switch no). however, when click toggle again, stays on yes yet executes javascript if being set yes. makes me think value of toggle is being set no when select radio button, toggle isn't displaying/animating toggle no. so maybe i'm asking how make toggle show animation of toggling no.
thanks!
figured out of friend.
i needed add "off" class parent element of toggle when radio button selected. had add 2 lines function, , works desired:
var parentind = $("#neutralind").parent(); parentind.addclass("off");
so here resulting function code:
function seteventsite(chosen) { var neutralind = document.getelementbyid("neutralind"); var locationoverride = document.getelementbyid("locationoverride"); var hometeam = document.getelementsbyname("hometeam"); var parentind = $("#neutralind").parent(); if (chosen == "neutral") { if (neutralind.checked) { locationoverride.readonly = false; for(var i=0; i<hometeam.length; i++) { hometeam[i].checked = false; } } else { locationoverride.value = ""; locationoverride.readonly = true; } } else if (chosen == "team") { neutralind.checked = false; neutralind.value="off"; parentind.addclass("off"); locationoverride.value = ""; locationoverride.readonly = true; } }
Comments
Post a Comment