Hiding a datepicker using jQuery

做~自己de王妃 提交于 2019-12-05 16:37:02

To hide a datepicker you need to

  • destroy the datepicker functionality from the input text field
  • hide the input text field

To show a datepicker you need to

  • show the input text field
  • add to it the datepicker functionality

Here is the demo: http://jsfiddle.net/ezKwN/

function hideIt(){
    $( "#frdate" ).datepicker( "destroy" );
    $( "#frdate" ).hide();
}

function showIt(){
    $( "#frdate" ).show();
    $( "#frdate" ).datepicker();
}

I don't know if this works for Struts2 jQuery datepicker too, but i hope so.

But consider that using that tag, you are hard-coding that funcionality to the page, it is not supposed to be dynamic, then (if the above solution doesn't work), if you need to show / hide it according to user interactions, you should consider using the native jQuery datepicker instead of the Struts2 one (only for the dynamic datepicker)

EDIT: as another option (with an smaller impact than recoding all your datepickers with native jQuery), you can simply encapsulate the tag inside a <div>, and hide / show the div.

function hideIt(){
  $("#frdate").hide();
  $("label[for='frdate']").hide();
  $("#frdate").datepicker("destroy" );
 }

function showIt(){
  $("#frdate").show();
  $("label[for='frdate']").show();
  $("#frdate").datepicker({
   showOn : "both",
   buttonImage : "/path/struts/js/calendar.gif",
   maxDate : "0",
   jqueryaction : "datepicker",
   id : "frdate",
   name : "training.fromDate"
   });
 }

You can hide the jquery datepicker with one of two approaches:

  • Removing it entirely: $('input[type=text]#yourInput').datepicker("destroy");
  • Hiding it: $('input[type=text]#yourInput').datepicker("hide");
    • Hiding it is probably better, so you don't have to re-initialize it with all the parameters you want.

You may also hide the input, if you want: - $('input[type=text]#yourInput').hide();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!