javascript - Custom checkbox not working in Chrome and Safari browser -
i used below code custom checkbox,
html
<div class="agree"> <label for="agree_check" class="label_check"><input type="checkbox" class="agree_check" id="agree_check" />agree</label> </div>
css
.has-js .label_check { padding-left: 34px; } .has-js .label_check { background: url(../images/box1.png) no-repeat; } .has-js label.c_on { background: url(../images/box1-with-mark.png) no-repeat; } .has-js .label_check input { position: absolute; left: -9999px; }
script
<script type="text/javascript"> var d = document; var safari = (navigator.useragent.tolowercase().indexof('safari') != -1) ? true : false; var gebtn = function(parel,child) { return parel.getelementsbytagname(child); }; onload = function() { var body = gebtn(d,'body')[0]; body.classname = body.classname && body.classname != '' ? body.classname + ' has-js' : 'has-js'; if (!d.getelementbyid || !d.createtextnode) return; var ls = gebtn(d,'label'); (var = 0; < ls.length; i++) { var l = ls[i]; if (l.classname.indexof('label_') == -1) continue; var inp = gebtn(l,'input')[0]; if (l.classname == 'label_check') { l.classname = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off'; l.onclick = check_it; }; if (l.classname == 'label_radio') { l.classname = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off'; l.onclick = turn_radio; }; }; }; var check_it = function() { var inp = gebtn(this,'input')[0]; if (this.classname == 'label_check c_off' || (!safari && inp.checked)) { this.classname = 'label_check c_on'; if (safari) inp.click(); } else { this.classname = 'label_check c_off'; if (safari) inp.click(); }; }; var turn_radio = function() { var inp = gebtn(this,'input')[0]; if (this.classname == 'label_radio r_off' || inp.checked) { var ls = gebtn(this.parentnode,'label'); (var = 0; < ls.length; i++) { var l = ls[i]; if (l.classname.indexof('label_radio') == -1) continue; l.classname = 'label_radio r_off'; }; this.classname = 'label_radio r_on'; if (safari) inp.click(); } else { this.classname = 'label_radio r_off'; if (safari) inp.click(); }; }; </script>
now need check checkbox checked or not.. works in ff not works in chrome , safari browser.
if (jquery('#agree_check').is(':checked')) { alert("checked"); } else { alert("not checked"); }
when checked checkbox displays "checked" in ff.. in chrome , safari displays "not checked".
what issue?
my solution, working on server side - codebehind (runat="server" attribute label , checkbox), consist add checked/unchecked state critical browser. maybe work browser, clearing if
statement.
var check_it = function () { var inp = gebtn(this, 'input')[0]; if (this.classname == 'label_check c_off' || (!safari && inp.checked)) { this.classname = 'label_check c_on'; if (chrome) inp.checked = true; if (safari) inp.click(); } else { this.classname = 'label_check c_off'; if (chrome) inp.checked = false; if (safari) inp.click(); }; };
Comments
Post a Comment