Skip to content
Loading
  
  •   
  •                   
  •                 "text/javascript">  
  •                     $(document).ready(function () {  
  •                         $('#checkBox').on('click', onClickCheckBox);  
  •                     });    
  •             function onClickCheckBox() {      
  •         if($(this).is(":checked")) {      
  •         $("#datepicker").show();      
  •         }     
  •         else     
  •         {      
  •        $("#datepicker").hide();    
  •     }     
  •  }  
  •                   
  •  
    +2
  • Junaid Musla
    +2
    Hi,

    Try to write your script in the script section as below:
    1. @section ScriptSection{  
    2.       
    3.     "text/javascript">  
    4.   
    5.         $(document).ready(function () {       
    6.   
    7.             $('#Lockuntil').on('click', onClickCheckBox); // bind an event with checkbox on click. On click function onClickCheckBox will be called.  
    8.   
    9.             onClickCheckBox(); // to hide or show on page load.c  
    10.         });  
    11.   
    12.         function onClickCheckBox() // logic to show hide textbox based on checkbox's value.  
    13.         {  
    14.             if ($('#Lockuntil').is(":checked")) {  
    15.                 $("#TextValue").show();  
    16.             }  
    17.             else {  
    18.                 $("#TextValue").hide();  
    19.             }  
    20.         }  
    21.       
    22. }  
     
    Now, here is the code of your Ui control:
    1. <div class="row">  
    2.    <div class="form-group">  
    3.       <div class="col-sm-2">  
    4.           @Html.CheckBoxFor(model => model.Lockuntil, new { @class = "rb", id = "Lockuntil" })  
    5.           @Html.TextBoxFor(model => model.TextValue, new {  @class="datepicker", id = "TextValue" })  
    6.       div>  
    7.    div>  
    8. div>  

    I have tested this with model and it works fine. Datepicker is also visible.

    Screenshot: 
     
    +2
  • +2
    Hi Junaid
     
    Ok, the check and unchecking is working, but the Textbox is not displaying date, must change its data type from the model? as being Datetime?
    1. class="form-group row">  
    2.                 class="col-sm-2">  
    3.                     @Html.CheckBoxFor(model => model.Lockuntil, new { @class = "rb", id = "Lockuntil" })  
    4.   
    5.                 
      
  •                 class="form-group row">  
  •                     class="col-sm-1">  
  •                         @Html.TextBoxFor(model => model.DateSelect, new { @class = "datepicker", id = "TextValue" })  
  •                       
  •                   
  •               
  •   
  •               
  •             "text/javascript">  
  •                 $(document).ready(function () {  
  •   
  •                     $('#Lockuntil').on('click', onClickCheckBox); // bind an event with checkbox on click. On click function onClickCheckBox will be called.  
  •   
  •                     onClickCheckBox(); // to hide or show on page load.c  
  •                 });  
  •   
  •                 function onClickCheckBox() // logic to show hide textbox based on checkbox's value.  
  •                 {  
  •                     if ($('#Lockuntil').is(":checked")) {  
  •                         $("#TextValue").show();  
  •                     }  
  •                     else {  
  •                         $("#TextValue").hide();  
  •                     }  
  •                 }  
  •               
  •  
    +2
  • Junaid Musla
    +2
    Hey,
     
    So basically, I prefer to use string datatype for Date when it comes to web development UI.
    Below is the property which i used in viewModel: 
    1. public string  TextValue { get; set; }    
     Try this out.
    +2
  • +2
    Hi Junaid, i cant seem to know exactly on my side the issue is here, i still getting no datepicker when checking. The code is exactly same no error when debugging, perhaps what could be the problem let me share my GUI with you here.
    +2
  • +2
    Hi Junaid please send full javascript code,thanks for datepicker one just send now.
    +2