Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncheck all checkbox on pageload using javascript

Tags:

javascript

I am using following code but it is not working. Tell me if any suggestion. I want all chekboxes unchecked when I load the page. I've got the following code but it doesn't work:

 window.onload = function abc() {
     document.getElementsByTagName('input')[0].focus();
 }
<tr>
    <td>
        <input type="checkbox" ID="cb1" value="29500" onclick="if(this.checked){ cbcheck(this) } else { cbuncheck(this)}" /> Laptop
    </td>
    <td>
        <a href="#" id="a1" onmouseover="showimage('a1','laptop1');"  >Show Image</a>
        <img src="Images/laptop.jpg" id="laptop1" alt="" style="display:none; width:150px; height:150px;" onmouseout="hideimage('a1','laptop1');" class="right"/>
    </td>
 </tr>
 <tr>
     <td>
          <input type="checkbox" ID="cb2" value="10500" onclick="if(this.checked){ cbcheck(this) } else { cbuncheck(this)}" /> Mobile
     </td>
     <td>
          <a href="#" id="a2" onmouseover="showimage('a2','mobile1');"  >Show Image</a>
          <img src="Images/mobile.jpg" id="mobile1" alt="" style="display:none; width:150px; height:150px;"   onmouseout="hideimage('a2','mobile1');" />
     </td>
</tr>
like image 501
kkk Avatar asked Nov 29 '22 09:11

kkk


2 Answers

Call this function on your page load event

function UncheckAll(){ 
      var w = document.getElementsByTagName('input'); 
      for(var i = 0; i < w.length; i++){ 
        if(w[i].type=='checkbox'){ 
          w[i].checked = false; 
        }
      }
  } 
like image 105
Talha Avatar answered Dec 15 '22 07:12

Talha


You should try

window.onload = function(){
   var checkboxes = document.getElementsByTagName("INPUT");

   for(var x=0; x<checkboxes.length; x++)
   {
      if(checkboxes[x].type == "checkbox")
      {
          checkboxes[x].checked = false;
      }
   }

}

and If you can use jQuery, you can try

$(function(){
    $('input[type=checkbox]').prop("checked", false);
});
like image 45
Yograj Gupta Avatar answered Dec 15 '22 07:12

Yograj Gupta