Solved

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

Posted on 2011-02-26
3
679 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
control character echo in html input field with javascript or jquery 7 33
Syntax Error 2 44
Replace &lt; with < 14 54
Run time Error 4 34
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

20 Experts available now in Live!

Get 1:1 Help Now