Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the week number of the date picked using jquery datepicker

I have the following code for selecting a date which I then want to convert to a week number.

    $(".calendar").datepicker({
        showWeek: true,
        onSelect: function(dateText, inst) {
            dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)),
            alert($.datepicker.iso8601Week(new Date(dateText)))
        }
    });

The alert shows the correct week number, but the date does not reformat at all.

I can't move the dateFormat function outside of the onSelect because this causes nothing to happen.

What I would like to achieve is the text field to say something like "Week Number 13"

Any ideas?

http://jsfiddle.net/ENG66/

like image 934
Tom Avatar asked Sep 20 '12 15:09

Tom


Video Answer


2 Answers

I've updated the fiddle to make it work: http://jsfiddle.net/ENG66/11/

In the onselect event, you need to use .val() to override the setting of the textbox value

$(".calendar").datepicker({
        showWeek: true,
        onSelect: function(dateText, inst) {
            $(this).val("'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)));
        }
    });​

EDIT

As Igor Shastin pointed out in his comment below we already have the text box in inst.input

inst.input.val($.datepicker.iso8601Week(new Date(dateText)));
like image 136
James Wiseman Avatar answered Oct 07 '22 02:10

James Wiseman


Try this jsFiddle example.

$(".calendar").datepicker({
    showWeek: true,
    onSelect: function(dateText, inst) {
        dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)),
        $(this).val('Week:' + $.datepicker.iso8601Week(new Date(dateText)));
    }
});​
like image 20
j08691 Avatar answered Oct 07 '22 01:10

j08691