Skip to content Skip to sidebar Skip to footer

Set Select List To Current Date Using Jquery

I am using Jquery mobile, and I have these two select lists, I want to set the selected options for the to lists to the current day and month .. edit I am using JqueryMobile the co

Solution 1:

The problem with your code is that .get() on a jquery object returns the DOMElement at the index specified and not a jquery object anymore, so you cannot use .attr() on it (source).

Here's a working piece of code:

$('#select-choice-day option[value=' + day + ']').prop('selected',true);
$('#select-choice-month option[value=' + (month+1)  + ']').prop('selected',true);

Please note that .prop() is only available for jquery 1.6 and above. If you are using an older version of the library, use attr() as you do.

Here's a jsfiddle

Solution 2:

Try

var today = newDate();
$('#select-choice-day').val( today.getDate() );
$('#select-choice-month').val( today.getMonth() + 1 );

Remember to add 1 to .getMonth() since it is zero-indexed.

Here is an example.

Solution 3:

try this-

$('#select-choice-day option[value=' + day + ']').attr('selected',true);
$('#select-choice-month option[value=' + (month+1)  + ']').attr('selected',true);

Working Example

Post a Comment for "Set Select List To Current Date Using Jquery"