Yasin Yörük
Yasin Yörük

Reputation: 1539

Add option into dynamically created selectbox

I have to create new select box when click the create button and add options into this dynamically created select box. How can I do that ?

$(".create").live('click',function() {
    var dates = '<option value="2013-03-1">2013-03-1</option>\n';
    var row = '<tr>
<td>date1 - date2</td><td><select name="tarih1_single[]" class="date1"></select> - <select name="tarih2_single[]" class="date2"></select></td></tr>';

    for (i = 2; i <= 30; i++) {
        dates += '<option value="2013-03-'+i+'">2013-03-'+i+'</option>\n';  
    }
    $(".date1").append(dates);
    $(".date2").append(dates);
    $("#konaklama").append(row);
});

------ EDIT ----- I changed only last 3 rows order.

$("#konaklama").append(row);
$(".date1").append(dates);
$(".date2").append(dates);

Upvotes: 1

Views: 1637

Answers (2)

Alex
Alex

Reputation: 349

Try this,at first add row to page and then append dates: http://jsfiddle.net/sT3pS/

UPD#1 I think that you wanted format like this, just add style for date header centering : http://jsfiddle.net/sT3pS/1/

Upvotes: 1

wroniasty
wroniasty

Reputation: 8052

$('#select_id')
   .append ( $('<option/>')
      .attr('value', 'valueOfOption')
      .html('text of option')
   )

Upvotes: 0

Related Questions