Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-26
3
Medium Priority
?
714 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1920 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 80 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

604 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