Click here to Skip to main content
Licence CPOL
First Posted 25 May 2011
Views 8,020
Bookmarked 4 times

Making a jQuery UI DatePicker Read Only

By | 25 May 2011 | Technical Blog
Block any way for the user to change the textbox value in the DatePicker, either from the calendar popup or by direct input on the textbox.
A Technical Blog article. View original blog here.[^]

To make this post clear, by ReadOnly I mean really read only, no input allowed whatsoever.

DatePicker default behavior

This one seemed easy at first but it isn't just because by design, setting the input textbox to readonly, the widget will understand it as if we just wanted to limit the user to choose a date from the popup, disallowing any input text.

What I want here is to block any way for the user to change the textbox value, either from the calendar popup or by direct input on the textbox.

Option 1: Disable Textbox

The good The bad
Works! Grays out the control to make it look disabled

This would be a quick solution by just disabling the textbox but it kind of shades the control, and I want it to display its value with the same appearance as the other textboxes. Not approved!

Option 2: Bend the control

This DatePicker widget has a lot of options, we just have to find a way to use them in our favor to achieve this "unsupported" behavior.

<input type="text" id="txtDate" readonly="readonly" />

<script type="text/javascript">

$(document).ready(function () {
   $('#txtDate').datepicker(
     {
        beforeShow: function (input, inst) 
        { inst.dpDiv = $('<div style="display: none;"></div>'); }
     });
});

</script>

What did I do?

On the textbox you can see that I made it read-only, but as I said, this alone still lets the user change the date from the calendar popup. To block the calendar popup, I'm replacing it with an empty DIV element, and to avoid unhandled complications, I'm styling it as display: none.

With this workaround, the DatePicker still thinks it is showing the popup but in fact nothing happens.

This is kind of tricky but is pretty easy to do and works very well on all browsers.

My recommendation to the jQuery UI team

This would be so much easier if we need only pass something like: showOn: "never".

License

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

About the Author

AlexCode

Architect
AlexCode
Portugal Portugal

Member

Alexandre Simões is a freelancer working as senior software architect with the focus on Financial Web Applications.
 
Find more about me on my blog.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberJohn Kasra12:12 15 Jun '11  
GeneralWhy? PinmemberRichard Deeming3:59 31 May '11  
GeneralRe: Why? PinmemberAlexCode5:25 31 May '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 May 2011
Article Copyright 2011 by AlexCode
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid