javascript - Check All won't work in Chrome but works in Firefox -
i have check box wherein when user tick it, items under ticked. works fine in firefox won't perform check function in chrome.
this js function:
function check(chk, num) { if(chk.value=="check all"){ (i = 0; <= num; i++){ chk[i].checked = true ; } chk.value="uncheck all"; }else{ (i = 0; <= num; i++){ chk[i].checked = false ; } chk.value="check all"; } }
html:
<form target="_blank" action="" method="post" id="myform" name="myform"> <input type="checkbox" value="check all" onclick="check(document.myform.product a, 9)" id="fujitsu" name="fujitsu"> select <input type="hidden" value="1" name="product_id[1]"> <input type="checkbox" value="product 1" id="product 1" name="product[1]">product -product 1 <input type="hidden" value="2" name="product_id[2]"> <input type="checkbox" value="product 2" id="product 2" name="product[1]">product -product 2 </form>
for me both firefox , chromium crash on "document.myform.product a". here code little modified work:
function check(form, all, chk, num) { if(form[all].value=="check all"){ (i = 1; <= num; i++){ form[chk + i].checked = true ; } form[all].value="uncheck all"; }else{ (i = 1; <= num; i++){ form[chk + i].checked = false ; } form[all].value="check all"; } }
and html:
<form target="_blank" action="" method="post" id="myform" name="myform">- <input type="checkbox" value="check all" onclick="check(document.myform, 'fujitsu', 'product_' , 2)" id="fujitsu" name="fujitsu"> select all- <input type="hidden" value="1" name="product_id[1]">- <input type="checkbox" value="product 1" id="product_1" name="product[1]">product -product 1- <input type="hidden" value="2" name="product_id[2]">- <input type="checkbox" value="product 2" id="product_2" name="product[1]">product -product 2- </form>
but can cleaner code same thing:
function check(form, action) { var l = form.getelementsbytagname("input"); (var = 0; < l.length; ++i) if (l[i].type == "checkbox") l[i].checked = action; }
and html:
<form target="_blank" action="" method="post" id="myform" name="myform">- <input type="checkbox" value="check all" onclick="check(document.myform, this.checked)" id="fujitsu" name="fujitsu"> select all- <input type="hidden" value="1" name="product_id[1]">- <input type="checkbox" value="product 1" id="product_1" name="product[1]">product -product 1- <input type="hidden" value="2" name="product_id[2]">- <input type="checkbox" value="product 2" id="product_2" name="product[1]">product -product 2- </form>
it better if used library css selectors jquery.
Comments
Post a Comment