jeudi 10 juin 2021

How to get next checkbox checked value in jQuery

I'm trying to implement auto update some of the columns in my sql server database. I have couple of dropboxs and html switches in a form. I'm trying to get the element id and selected value of those elements.

This is the html tag:

                                <li class="list-group-item"><b>Market</b><asp:DropDownList ID="ddlRecruitingMarket" runat="server" CssClass="pull-right sidebar-controls action">
                                    <asp:ListItem Selected="True" Value="NA">Select One</asp:ListItem>
                                    <asp:ListItem>India</asp:ListItem>
                                    <asp:ListItem>Bangladesh</asp:ListItem>
                                    <asp:ListItem>Bhutan</asp:ListItem>
                                    <asp:ListItem>Myanmar</asp:ListItem>
                                    <asp:ListItem>Mauritius</asp:ListItem>
                                    <asp:ListItem>Nepal</asp:ListItem>
                                    <asp:ListItem>Maldivs</asp:ListItem>
                                    <asp:ListItem>Sri Lanka</asp:ListItem>
                                </asp:DropDownList>
                                </li>
                                <li class="list-group-item"><b>Do Not Call</b>
                                    <a class="pull-right">
                                        <label for="chkDoNotCall" class="switch action">
                                            <asp:CheckBox ID="chkDoNotCall" runat="server" /><span class="slider slider-red round"></span></label></a>
                                </li>
                                <li class="list-group-item"><b>Do Not Message</b>
                                    <a class="pull-right">
                                        <label for="chkDoNotMessage" class="switch action">
                                            <asp:CheckBox ID="chkDoNotMessage" runat="server" /><span class="slider slider-red round"></span></label></a>
                                </li>
                                <li class="list-group-item"><b>Do Not Email</b>
                                    <a class="pull-right">
                                        <label for="chkDoNotEmail" class="switch action">
                                            <asp:CheckBox ID="chkDoNotEmail" runat="server" /><span class="slider slider-red round"></span></label></a>
                                </li>

Now, what i want is when i click on the checkbox, i should be able to get the current checkbox id and the value i.e. true or false.

Please note, I'm able to get this for dropdown list right but not working for checkboxes.

Here is my jQuery function for the same.

               $(".action").on("change", function () {
                    var elVal = "";
                    if ($(this).hasClass("switch")) {
                        var elmid = $(this).attr("for");
                        elVal = $(this).next("input:checkbox").is(":checked");
                    } else {
                        var elmid = $(this).attr("id");
                        elVal = $(this).val();
                    }
                    alert("Current Element: " + elmid + " | Value: " + elVal);
                });

Kindly help me to solve this issue.

thanks in advance.




Aucun commentaire:

Enregistrer un commentaire