dimanche 23 juillet 2017

jQuery conditional checkbox property inside a repeater

Hi all this is a simple and stupid one as I'm not smart enough to figure it out. So I have the following ordered list inside an asp:Repeater --

<ul id="KeyAreaUl">
   <asp:Repeater ID="Rptr" runat="server">
      <ItemTemplate>
         <li runat="server" id="ActiveLiElement" class='<%# IsActive(DataBinder.Eval(Container.DataItem, "Key").ToString())%>'>
            <asp:CheckBox Text='<%# DataBinder.Eval(Container.DataItem, "Key")%>' runat="server" ID="KeyCheckbox" Checked="false" />
            <a href="#"><%# DataBinder.Eval(Container.DataItem, "Key") %></a>
         </li>
      </ItemTemplate>
   </asp:Repeater>
</ul>

I'm adding/removing class active to the li element from code behind based on current querystring. The goal is to write a jQuery script that sets the Checkbox property to Checked if the corresponding li has class active.

My attempt (an unsuccessful one) was --

<script type="text/javascript">
    $(document).ready(function () {
        if ($('#KeyAreaUl li').hasClass('active')) {
          $(this).find('input').prop('checked', true);               
        };
     });
</script>

The Problem I'm facing is, the script sets all the checkboxes as checked if a li element has active class. Whereas the goal is to set the checkbox checked only when its parent li has class active. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire