views:

47

answers:

1

I've got a datepicker in an asp.net web app that has today's date set by default. We have an icon that when clicked needs to clear this date (and potentially add 'dd/mm/yyyy' as text back in as some descriptive text on date entry). I've added an icon and created a click event handler on it but I'm having issues setting the date to nothing.

$('#dt-clr').click( function(){
    $(this).closest(".datepicker").val('dd/mm/yyyy');
});

I've even tried 'destroy' in the event handler above and ('setDate', '') but if that's the best way I mustn't be referencing it correctly.

Any ideas? :s

UPDATE: Eventually realised my icon was on the same DOM level as the datepicker so closest wasn't working (Gaby was onto this too, see comments below). So my code:

$(this).siblings(".datepicker").datepicker( 'setDate' , null )

...strips out the date and leaves me with a blank date textbox. Just need to figure out how to add the string 'dd/mm/yyyy' in instead of my empty box.

UPDATE 2: Ok, this works. There's probably a better way, if there is please let me know:

$(this).siblings(".datepicker").datepicker( 'setDate' , null );
$(this).siblings(".datepicker").val('dd/mm/yyyy');
+2  A: 
Gaby
Yes, doesn't work.
lloydphillips
Check the example i just added ..
Gaby
Hey Gaby, figured that much out and then saw your update. Doh! Ok, so I figured out that it was an issue with the 'closest' since the icon was a sibling so I've edited my post. I've still got an issue in that I wanted to set the text in the data as 'dd/mm/yyyy' whereas null clears the textbox. I'll keep working at it but I'll keep a closer eye on any updates this time. :
lloydphillips
Sorted it. Did a second update. If anyone has a more elegant solution I'd love to know. There most likely is one! :)Thanks Gaby!
lloydphillips