Search code examples
jquerydateselected

if (day = day select) in jQuery


 $('<option></option>')
            .attr('label', day)
            .attr('value', day)
            if(myDate.getDate() == day)
            {
            .attr('selected','selected')    
            }
            .html(day)
            .appendTo(daySelector); 

What I want to do is run an if statement and if it is true make that day selected.


Solution

  • You can use an object to accomplish this:

    $('<option />', {
        selected: myDate.getDate() === day,
        value: day,
        text: day
    }).appendTo(daySelector)
    

    Here is an example of that style: http://jsfiddle.net/9hVeq/