javascript - When "check-all" box checked, check others -
i have form located on html page bunch of checkboxes options. 1 of options "check-all" , want other check boxes checked, if unchecked, "check-all" box checked. code looks this:
<form method = "post" class = "notification-options"> <input type = "checkbox" name = "notification-option" id = "all-post" onclick = "javascript:checkall(this );"> posts <br/> <input type = "checkbox" name = "notification-option" id = "others-post"> other's posts <br/> <input type = "checkbox" name = "notification-option" id = "client-post"> cilent's post <br/> <input type = "checkbox" name = "notification-option" id = "assign-post"> task assigned </form> java script:
<script type = "text/javascript"> var $check-all = document.getelementbyid("all-post"); function checkall($check-all){ if ($check-all.checked == true){ document.getelementbyname("notification-option").checked = true; } } </script> nothing happens when run code
here guidelines.
type attribute not needed , can omitted. js variable names can't contain hyphens, typo in getelementbyid() you're using global variable name argument, in same time you're passing this online handler. passed argument shadows global within function. if (checkall.checked) job typo in getelementsbyname(), gebn() returns htmlcollection, array-like object. you've iterate through collection, , set checked every element separately. fixed code:
<script> var checkall = document.getelementbyid("all-post"); function checkall(){ var n, checkboxes; if (checkall.checked){ checkboxes = document.getelementsbyname("notification-option"); (n = 0; n < checkboxes.length; n++) { checkboxes[n].checked = true; } } } </script> you can omit javascript: pseudo-protocol , argument online handler.
javascript html
No comments:
Post a Comment