views:

258

answers:

3

Hi,

Please help I am trying to code for selectAll or clear all using Jquery. I have a checkboxlist and the value of id seems to be incorrect. Id in firebug shows the value "<%= checkboxId.ClientID %>"

Thanks SA

Part of the code:

<a href="javascript:void(0);" onclick="selectClearAlltest('<%= checkboxId.ClientID %>', true)">Select All</a> |
<a href="javascript:void(0);" onclick="selectClearAlltest('<%= checkboxId.ClientID %>', false)">Clear All</a>
<asp:checkboxList id="checkboxId" runat="server" />

Script:

    function selectClearAlltest(id, value) {
        var chks = $("#" + id + ":input");
        chks.attr("checked", value);
    }
+1  A: 

Your selector is wrong, it makes #checkboxId:input. You should add another space (you're looking for descendants), and better:

var chks = $("#" + id + " input:checkbox");
Kobi
I have a asp:checkboxlist. So should I replace it with " input:checkoboxlist" then?
sa
Nope. `asp:checkboxlist` is a *server side* object, it doesn't exist as such on the client side - it is transformed to regular checkboxes - `<input type='checkbox' />`. You can use `View Source` on your page to see that.
Kobi
okay! You are right. It is a checkbox.
sa
What's strange is the checked property is set to "checked" even though I had unchecked it and done a vew source. I though the value for check property is true, false or undefined.
sa
it is better to remove the attr `.removeAttr("checked")` instead of setting it to false
Tzury Bar Yochay
A: 

adding a class name would make things simpler e.g.

<asp:checkboxList id="checkboxId" runat="server" CssClass="whatever"/>

and the jQuery code:

function selectClearAlltest(value) {
       $(".whatever").attr("checked", value);
}

or even better using jQuery.toggle as

$("#select_all_element_id").toggle(
  function () {
    $(".whatever").attr("checked", "checked");
  },
  function () {
    $(".whatever").removeAttr("checked");
  },
);

Example:

<html>
    <head>

    </head>
    <body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
    <a id="toggle-select" href="#">select/deselect all</a>
    <hr/>
        <input type="checkbox" class='foo'>Click</input>
        <input type="checkbox" class='foo'>On</input>
        <input type="checkbox" class='foo'>The</input>
        <input type="checkbox" class='foo'>The</input>
        <input type="checkbox" class='foo'>Link</input>
        <input type="checkbox" class='foo'>Above</input>
        <input type="checkbox" class='foo'>To</input>
        <input type="checkbox" class='foo'>Change</input>
        <input type="checkbox" class='foo'>My</input>
        <input type="checkbox" class='foo'>Attributes</input>        

    <script>
        $(document).ready(function(){
            $("#toggle-select").toggle(              
              function () {
                $(".foo").attr("checked", "checked");
              },            
              function () {
                $(".foo").removeAttr("checked");
              }
            );
        });
    </script>
   </body>
Tzury Bar Yochay
That's `CssClass="whatever"`, and `$('.whatever input:checkbox')`
Kobi
I am not that familiar with asp.net but I assumed it would never be as simple as class= ;-). fixing it inline tough
Tzury Bar Yochay
Not there yet, `.whatever` is a container of many check boxes, you cannot set it's `checked` attribute (well, I guess you can, but to no effect...).
Kobi
this is so not correct sample code pasted to the answer
Tzury Bar Yochay
A: 

If Firebug is showing you <%= and %> then it means your page was not parsed/interpreted by the server properly.

These character sequences are usually meant to signify server-processed code, and not make their way into the actual markup. For example, are you trying to execute ASP code in an ".html" file? Or, ... are you trying to execute ASP code on a PHP-based server?

Another way to verify this is to just do a view source on the page, and see if your server-generated code is actually being generated!

Best of luck!!
-Mike

Funka