user3071591
user3071591

Reputation: 83

append to select option

I have built a select option listbox,

I want to append on button click into the listbox where, textbox1 will be value and textbox2 will be text.

Can anyone make this work? thanks.

http://jsfiddle.net/6y54P/4/

$("#listbox1").change(function() {
   var svalue = $("#listbox1 option:selected").val();
   var stext = $("#listbox1 option:selected").text();
    $("#textbox1").val(svalue);
    $("#textbox2").val(stext);
});

$("#button1").click(function(){
   var svalue = $("#textbox1").val(svalue);
   var stext = $("#textbox2").val(stext);
   $('<option value="'+ svalue +'"').html(stext).appendTo("#listbox1");
});

Upvotes: 0

Views: 104

Answers (2)

codingrose
codingrose

Reputation: 15699

Try:

$("#listbox1").change(function() {
   var svalue = $("#listbox1 option:selected").val();
   var stext = $("#listbox1 option:selected").text();
    $("#textbox1").val(svalue);
    $("#textbox2").val(stext);
});

$("#button1").click(function(){
   var svalue = $("#textbox1").val();
   var stext = $("#textbox2").val();
   $('<option value="'+ svalue +'">'+svalue+'</option>').html(stext).appendTo("#listbox1");
});

Updated fiddle here.

Upvotes: 2

Lucky Soni
Lucky Soni

Reputation: 6878

Instead of $("#listbox") use $("#listbox1") // in jsfiddle

Inside your button handler you need to fetch the values again

var svalue = $("#listbox1 option:selected").val();
var stext = $("#listbox1 option:selected").text();

Upvotes: 1

Related Questions