1

How can I get data of a datapicker in jQuery ui? I've the next code but only get default value. (I use only years)

$('#datePickerTempe').datepicker( {
    changeMonth: false,
    changeYear: false,
    dateFormat: 'MM yy',
    //To hide months:
    stepMonths: 12,  
    monthNames: ["","","","","","","","","","","",""], 
    //Range
    minDate: '-4y',  //'M'-> month | 'w'-> week | 'd'-> day
    maxDate: '0y',

    buttonImageOnly: true 
});

$('#datePickerTempe').on('click', function(){ 
console.log('año: '+$('#datePickerTempe').val()); });
4
  • Using a datepicker to select a year is quite an unusual solution... wouldn't a simple dropdown menu be easier for the user? Commented Jan 26, 2013 at 11:20
  • I prefer datepicker because jqueryui combobox doesn't work fine Commented Jan 26, 2013 at 11:55
  • Do you know another similar option? Commented Jan 26, 2013 at 12:00
  • <select type="multiple"> is a combobox. Commented Jan 26, 2013 at 20:09

3 Answers 3

1

Get current date from datepicker (or null if nothing selected):

var selectedDate = $('#datePickerTempe').datepicker("getDate");

You can also add onSelect handler to datepicker:

$('#datePickerTempe').datepicker( {
    onSelect: function (dateText, inst) {
        console.log(dateText);
    }
});

Refer to datepicker getDate method and datepicker onSelect option

Sign up to request clarification or add additional context in comments.

3 Comments

I think that it doesn't work because the value isn't reloaded if a day is selected. And I don't use days.
@vicenrele perhaps I don't understand, but value is changing every time you select new date. I have even used your options in my sample and it works, it shows correct date, check it here. I think you are asking wrong question, what exacly you are trying to achive?
Check this out: link If you don't select a day, the value doesn't change. It changes when a day is selected. I'll solve the problem using another option without datepicker
0
<input type="text" id="date" name="date" value=""/>    

<script>
    var date_fields = $('#date');
    date_fields.datepicker({
        format: 'mm/dd/yyyy'
    }).on('changeDate', function(ev){
        date_fields.datepicker('hide');
        date_fields.blur();
    });
</script>

The added scripts http://www.eyecon.ro/bootstrap-datepicker/

Comments

0
$("#datepicker").change(function() {
    alert($(this).val());
});

Works perfectly fine here.

2 Comments

@vicenrele then stackoverflow.com/questions/6667149/… should do it.
I think that it doesn't work because the value isn't reloaded if a day is selected. And I don't use days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.