views:

71

answers:

1

Important Edit: As only computers can do, today it works as it should, without any changes to code. Go figure. I'll delete this if I'm able, as it helps nobody.


I've got a date text field I wish to only sometimes attach a DatePicker to, because I have some of my own text-handling scripts which handle partial date strings. Calling .remove or .destroy leaves text formatting behavior on the input field, however, which converts my "8" string into a "8/18/2010". Even worse, if I start deleting, it steadfastly assumes that, once I reach "8/18/20", I actually wanted "8/18/2020".

What would be the best way to completely, entirely, make-it-like-it-never-was remove the datepicker from my page? I can also use it if it just ignores text I enter entirely at all times, though in that case I'd prefer it to appear on a double-click / an image-as-button, not always.

edit:

this is all within a jqGrid, where 'selector' is a text box on a date column:

function showPicker(selector) {
    $(selector).datepicker({
        onClose: function() {
            $(selector).datepicker('remove'); 
            // or 'destroy' or $('.datepicker').remove(); or $(this).datepick('remove');
        }
    });
}

This prevents it from coming back, but not from manipulating my text field. No other code (that I'm aware of) is manipulating that field's contents, just jqGrid watching for the enter-key to send the data. Looking at the page's generated code, the datepicker div is even still at the bottom.

edit2: I get the exact same behavior if I do this:

<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.js"&gt;&lt;/script&gt;
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"  />
<script type="text/javascript">
$(document).ready( function(){
 $("#pickle").datepicker({
  onClose: function(){
   $(this).datepicker('remove');
  }
 });
});
</script>
<input type="text" id="pickle" />
</body>
</html>

That causes identical behavior to what I'm seeing, but changing it to 'destroy' works here but not on my page. Odd.

+2  A: 

depending on your situation, you could do it server side

Ex. in asp-like sytax

<% if( showDatePicker ) {%>
    $('#dp-div').DatePicker(); // or whatever
<% } %>

Edit
How about setting the dateFormat of the datepicker? ie

$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' });

you might be wanting

$( ".selector" ).datepicker({ dateFormat: '...' });
BioBuckyBall
Simple, I must agree. But this is all client-side, and that would require re-building *anything* containing the picker code, and probably watching for JS data too. It seems to be tacking onto the rest of the page elsewhere, which is probably what's hanging around after I supposedly remove / destroy it. Not sure if that'd handle it, unless I refreshed the whole page (too expensive).
Groxx
we need to see code to comment further then
BioBuckyBall
Edited, and added code :)
Groxx
@Groxx so is the idea that you want to be able to let the user type OR use a datepicker? and when the date picker is closed, you want the text to render with your css, not DatePicker's?
BioBuckyBall
@Lucas: My... css? Not entirely sure what you mean. I've edited again, and have found a reduced-case that duplicates my *behavior*, but not apparently my *cause*, if you want to see.
Groxx
@Groxx I'm just wasn't sure what behaviour you are seeing. Adding something to answer...
BioBuckyBall
@Lucas re recent edit: no, that just replaces my text with periods in both pieces of code. I'd also thought that might work after looking at the documentation, but it appears it's just a demo of the field, not anything useful. I've also tried 'constrainInput: false', but that's the default and changes nothing :\.
Groxx
@Lucas: and today it magically works. Literally magically, I launched visual studio, hit ctrl-F5, and it's behaving exactly like it should. As such, I'm going to remove / vote to remove this question, because it can't really help anyone. Thanks for the help though! edit: or not. Bah. Was hoping to not pollute SO further...
Groxx