Reputation: 83
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.
$("#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
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");
});
Upvotes: 2
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