Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link not working 6 41
ASP get fieds value 2 20
replacing inline javascript with jquery 4 44
REST call Failing 1 7
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

685 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