javascript - Field.length not working on a single checkbox -


i have form contains list of data, added checkbox in of each, when click on uppermost checkbox selects all. if have 1 row of data , click on upper checkboxes dont work. here script below.

////checking checkbox on page function checkall(field) {    // alert(field.length);     (i = 0; < field.length; i++){         field[i].checked = true ;     } }  ////unchecking checkbox on page function uncheckall(field) {     (i = 0; < field.length; i++){         field[i].checked = false;     } } function selectchk(e){     if(e.checked==true){         //alert("yeah");         checkall(document.paginate_list.list);                                       }else{         uncheckall(document.paginate_list.list);      }                                }  function applytoselected(){ var selectedval=""; var withselected=document.getelementbyid("withselected"); // if(withselected.value==1){    //if(document.getelementbyid("list")){    //    selectedval=document.getelementbyid("list").value;    //} for(i=0; i<document.paginate_list.list.length;i++){       if (document.paginate_list.list[i].checked) {           //alert(i);           selectedval +=document.paginate_list.list[i].value+",";        }   }        if(selectedval==""){           return false;       }    if(confirm("are sure want delete selected row?")){       $.ajax({           url: "../__lib__/paginglib.php?tabledeleteid="+selectedval,           context: document.body,           success: function(response,status,xhr){            if(response==1){               alert("deleted successfully");                window.location.reload();            }else{                alert(response);                alert("an error occurred while deleting, please try again!");            }           // alert(response);             }        });   }  } } 

html data

<form name="paginate_list" style="display:inline;" action="" method="post"> <table  class="pagingbody"> <tr> <th><input type=checkbox   onclick=selectchk(this)  ></th> <th>category title</th><th>actions</th> </tr> <tr class="even"> <td><input   name=list[]   type=checkbox  id=list   value=10></td> <td>web design</td> <td><a href=?page=cat&edit=10>edit</a> | <a href=#10>delete</a></td> <tr/> </table>  <table> <tr> <td> <select name="withselected" id="withselected">   <option value="0">with selected</option> <option value="1">delete</option> </select>  <input type="button" name="applytoselected" id="applytoselected" value="apply selected" onclick="applytoselected()" /> </td> <tr/> </table> </form> 

paginate_list.list represents name of form , list name.

this fiddle demonstrate what's happening

fiddle here

the reason why happening because browser generates nodelist when have more 1 element in list send function checkall(); otherwise normal input element.

i added instanceof check see type of object was.

to check in ie follow thread here on so: nodelist check in ie

good luck!


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -