Solved

jquery selector doesn't check/uncheck all checkboxes from ASP.NET checkboxlist

Posted on 2011-02-26
3
678 Views
Last Modified: 2012-05-11
I have a .NET form with two links (select all / none) that should check / uncheck all the checkboxes from a checkboxlist. Unfortunately, there's a bug in my selector and the checkboxes remain unchanged. My source is below.

Can someone tell me where my error is?

Thanks in advance.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
<script src='/MyLocalDomain/Scripts/jquery-1.4.4.min.js' language='javascript' type='text/javascript'></script>
<script src='/MyLocalDomain/Scripts/jquery-ui-1.8.7.custom.min.js' language='javascript' type='text/javascript'></script>
<script type="text/javascript">
function checkAll(pid, check) {
   $('#' + pID + ' input[type=checkbox]').attr('checked', check);
   return false;
}
</script>
</head>

<body id="ctl00_MainBody" bgcolor="#ffffff" leftmargin="0" topmargin="0">
<form name="aspnetForm" method="post" action="MyPage.aspx" id="aspnetForm">

<table>
   <tr>
      <td class="txt-bodyimportant" vAlign="top" align="right" >
      Tests:
      <br /><a id="lnkSelectAll" href="" onclick="return checkAll('tdTest', true);">Select All</a>
      <br /><a id="lnkSelectNone" href="" onclick="return checkAll('tdTest', false);">Select None</a>
      </td>
      <td id="tdTest">
         <table id="ctl00_MainContent_chklstTests" >
            <tr>
               <td>
                  <input id="ctl00_MainContent_chklstTests_0" type="checkbox" name="ctl00$MainContent$chklstTests$0" />
                  <label for="ctl00_MainContent_chklstTests_0">Test Zero</label>
               </td>
               <td>
                  <input id="ctl00_MainContent_chklstTests_7" type="checkbox" name="ctl00$MainContent$chklstTests$7" />
                  <label for="ctl00_MainContent_chklstTests_7">Test One</label>
               </td>
               <td>
                  <input id="ctl00_MainContent_chklstTests_13" type="checkbox" name="ctl00$MainContent$chklstTests$13" />
                  <label for="ctl00_MainContent_chklstTests_13">Test Two</label>
               </td>
            </tr>
         </table>
      </td>
   </tr>
</table>

</form>
</body>
</html>

Open in new window

0
Comment
Question by:ZekeLA
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 480 total points
ID: 34987953
You did'nt use the same case : pid and pID

function checkAll(pid, check) {
 $('#' + pID +

0
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 20 total points
ID: 34987981

...href="#" as well

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26849913.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

jQuery(document).ready(function() {
});

function checkAll(pid, check) {
   $('#' + pid + ' input[type=checkbox]').attr('checked', check);
   return false;
}

</script>

</head>
<body id="ctl00_MainBody" bgcolor="#ffffff" leftmargin="0" topmargin="0">
<form name="aspnetForm" method="post" action="MyPage.aspx" id="aspnetForm">

<table>
   <tr>
      <td class="txt-bodyimportant" vAlign="top" align="right" >
      Tests:
      <br /><a id="lnkSelectAll" href="#" onclick="return checkAll('tdTest', true);">Select All</a>
      <br /><a id="lnkSelectNone" href="#" onclick="return checkAll('tdTest', false);">Select None</a>
      </td>
      <td id="tdTest">
         <table id="ctl00_MainContent_chklstTests" >
            <tr>
               <td>
                  <input id="ctl00_MainContent_chklstTests_0" type="checkbox" name="ctl00$MainContent$chklstTests$0" />
                  <label for="ctl00_MainContent_chklstTests_0">Test Zero</label>
               </td>
               <td>
                  <input id="ctl00_MainContent_chklstTests_7" type="checkbox" name="ctl00$MainContent$chklstTests$7" />
                  <label for="ctl00_MainContent_chklstTests_7">Test One</label>
               </td>
               <td>
                  <input id="ctl00_MainContent_chklstTests_13" type="checkbox" name="ctl00$MainContent$chklstTests$13" />
                  <label for="ctl00_MainContent_chklstTests_13">Test Two</label>
               </td>
            </tr>
         </table>
      </td>
   </tr>
</table>

</form>
</body>
</html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:ZekeLA
ID: 34988057
Aargh! I should have spotted that. Thank you.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now