html - How to calculate number of checkboxs'Id in a table via Javascript -


i wanna total number of checkbox in html.

request: (1) javascript (2) checkbox id "id=idcheckbox", (3) each checkbox name reserved back-end using. not javascript. (4) naming each checkbox's name checkbox_1, checkbox_2,... number indicate serial number of checkbox.

the table html

<table class="form_table" border="0" cellpadding="0" width="750px" id="idroutinelisttable">    <tr>    <td style="align:center;" class="routinelistcell">    <input type='checkbox' name='checkbox_1' id='idcheckbox'></input>    <!-- may many checkbox here.-->    </td> </tr> </table>     

my js code, function check checkbox selected before deletion. if no checkbox selected, pops warning.

function beforedelete()  /*{{{*/  {     var checked=0;     var checkboxall=document.all["idcheckbox"];     //the error here, when there 1 checkbox, cannot length value,      //coz checkboxall not recognized array. however, if there more       //two checkboxes, function works.     alert (checkboxall.length);         for(var i=0;i<checkboxall.length;i++) {         if (checkboxall[i].checked==true) {             checked++;         }     }     if(checked==0) {         alert('please select item delete');         return false;     }  }  /*}}}* 

the problem has indicate in above code. idea solve this?

here full working sample using jquery:

<script src="../../scripts/jquery-1.5.1.js" type="text/javascript"></script> <script>     function beforedelete() {         if ($("input:checked").length == 0) {             alert('please select item delete');             return false;         }         return true;     } </script>   <form onsubmit="return beforedelete()">     <table class="form_table" border="0" cellpadding="0" width="750px" id="idroutinelisttable">        <tr>        <td style="align:center;" class="routinelistcell">        <input type='checkbox' name='checkbox_1' id='idcheckbox'></input>        <input type='checkbox' name='checkbox_2' id='idcheckbox'></input>        <input type='checkbox' name='checkbox_3' id='idcheckbox'></input>        <!-- may many checkbox here.-->        </td>     </tr>     </table>        <input type="submit" /> </form> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -