title: Use One Javascript Function For Multiple GridViews
body: <p>I have a JS function to toggle check state for all the checkboxes in a gridview. Now, there are two similar gridviews and so, would like to use the same function.</p>
  
  <blockquote>
    <p>This is Page structure:</p>
    
    <p>Radiobutton rb1, RadioButton rb2</p>
    
    <p>Gridview gv1, gridview gv2.</p>
    
    <p>rb1 selected, shows only gv1 and hides gv2 and vice versa.</p>
    
    <p>On page load, only rb1 is checked and gv1 is visble.</p>
  </blockquote>
  
  <p>The JS function works fine when used for just one gridview. I changed it to this and it doesnt work. Where am I going wrong.</p>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  function SelectAll(chkBox)
  {
    var chkAll = document.getElementById(chkBox);
    var elements;
    var g1 = document.getElementById('&lt;%=gv1.ClientID%&gt;');
    var g2 = document.getElementById('&lt;%=gv2.ClientID%&gt;');
    if(g1.style.visibility=='visible')
     {
      elements = document.getElementById('&lt;%=gv1.ClientID%&gt;')
              .getElementsByTagName("INPUT");
     }
    else if(g2.style.visibility=='visible')
     {
       elements = document.getElementById('&lt;%=gv2.ClientID%&gt;')
              .getElementsByTageName("INPUT");
     }
   for(var i=0;i&lt;elements.length;i++)
    {    
     var chk;
     if(elements[i]!=null)
      {
        if(elements[i].id.indexof('chkLeaveType')&gt;0)
            {
                 chk=elements[i];
                 chk.checked = chkAll.checked;    
            }            
      }
    }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>ASPX:</strong>
      
       
        
        
       
       
      
      
         
         <br>
          
           onclick="SelectAll(this.id)"/>Leave Type<br>
         
         
            
         
         
      
      </p>
  
  <pre><code>&lt;asp:gridview id="gv2" runat="server"&gt;
  &lt;columns&gt;
    &lt;asp:templatefield&gt; 
     &lt;headertemplate&gt;   
      &lt;asp:checkbox id="chkheader" runat="server" checked="true" 
       onclick="SelectAll(this.id)"/&gt;Leave Type  
     &lt;/headertemplate&gt;
     &lt;itemtemplate&gt;
        &lt;asp:checkbox id="chkLeaveType" runat="server" checked="true"/&gt;
     &lt;/itemtemplate&gt;
    &lt;/asp:templatefield&gt; 
  &lt;/columns&gt;
  &lt;/asp:gridview&gt;
  </code></pre>
  
  <p>
  </p>