Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I am trying to make a logic around, when my unchecked-box is used must clear the datepicker. Now its not doing that at all and need some help around it, please team.

What I have tried:

@using (Html.BeginForm("Modules", "Home", FormMethod.Post))
                                   {
                                       @Html.CheckBoxFor(model => model.Lockuntil, new { @class = "rb", data_toggle = "hide-element", data_target = "#datepicker17" })
                                       @Html.LabelFor(model => model.Lockuntil, new { @class = "form-check-label" })
                                       @Html.TextBoxFor(model => model.DateColumn, new { id = "datepicker" })
                                   }

<!--Jquery for checking datepicker-->
                    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
                    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
                    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
                    <script type="text/javascript">
                        $(function () {
                            var toggleHideElement = function (chk) {
                                var target = $(chk.getAttribute("data-target"));
                                if (chk.checked) {
                                    target.removeClass("d-none").attr("aria-hidden", "false");
                                }
                                else {
                                    target.addClass("d-none").attr("aria-hidden", "true");
                                }
                            };

                            // Change the state of the target element when the box is checked / unchecked:
                            $(document).on("change", "input:checkbox[data-toggle='hide-element']", function () { toggleHideElement(this); });

                            // Set the initial state of the target element:
                            $("input:checkbox[data-toggle='hide-element']").each(function () { toggleHideElement(this); });
                        });

                        // Another function call for DatePicker.
                        $(function () {
                            $("#datepicker").datepicker();
                        });
                    </script>
Posted
Updated 17-Sep-20 0:17am

1 solution

Hide the date picker when the box is not checked:
JavaScript
var toggleHideElement = function (chk, fromLoad) {
    var target = $(chk.getAttribute("data-target"));
    if (chk.checked) {
        target.removeClass("d-none").attr("aria-hidden", "false");
        if (!fromLoad) { target.focus().datepicker("show"); }
    }
    else {
        target.addClass("d-none").attr("aria-hidden", "true");
        if (!fromLoad) { target.datepicker("hide"); }
    }
};


// Change the state of the target element when the box is checked / unchecked:
$(document).on("change", "input:checkbox[data-toggle='hide-element']", function () { toggleHideElement(this, false); });

// Set the initial state of the target element:
$("input:checkbox[data-toggle='hide-element']").each(function () { toggleHideElement(this, true); });
 
Share this answer
 
v4
Comments
gcogco10 17-Sep-20 6:27am    
Ok Richard, what i want is when i click the check-box it must show me datepicker i have created on the second function, when i unchecked then it must clear the datepicker from the modal form, hope make clarity now sorry for not being clear.
Richard Deeming 17-Sep-20 6:32am    
Assuming you're using the jQuery UI datepicker, call its "show" method when the box is ticked, and call its "hide" method when the box is unticked:
Datepicker Widget | jQuery UI API Documentation[^]

You'll probably want to avoid doing that when the page loads.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900