Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-03-2008, 06:33 AM
PM User |
#1
Regular Coder
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
jQuery: Textbox value to Listbox
Hello,
I have the following:
- textbox (txt_RegionName)
- button (btn_AddToList)
- listbox (lst_Regions)
I want to add text to listbox which I enter in the textbox on click of the button.
For example, If I enter "xyz" in the textbox >> [click button] >> Its should add in the listbox.
I am using the following code to do this but getting a diff result. The whole of the textbox is getting listed in the listbox. (pic attached)
Code:
<script>
$(document).ready(function(){
$("#btn_AddToList").click(function(){
$('input[name=txt_RegionName]').appendTo("#lst_Regions");
});
});
</script>
Plz tell me a solution.
Thanx
11-03-2008, 09:14 AM
PM User |
#2
Senior Coder
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
To insert new option on the listbox, use the
Option() object of JS, I'm not aware of any jQuery counterpart on that. This might help:
Code:
$(document).ready(function(){
$("#btn_AddToList").click(function(){
var opt=document.getElementById('lst_Regions');
var inp=$('input[name=txt_RegionName]').val();
opt.options[opt.options.length]=new Option(inp,inp);
});
});
Users who have thanked rangana for this post:
11-03-2008, 09:34 AM
PM User |
#3
Senior Coder
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Does this work?
Code:
<script>
$(document).ready(function(){
$("#btn_AddToList").click(function(){
$('#lst_Regions').append('<option value=\"'+$('input[name=txt_RegionName]').val()+'\">'+$('input[name=txt_RegionName]').val()+'</option>');
});
});
</script>
ETA: Aha. I'd use the solution posted above this
Last edited by Spudhead; 11-03-2008 at 09:35 AM ..
Reason: n00bness
Users who have thanked Spudhead for this post:
11-03-2008, 09:43 AM
PM User |
#4
Regular Coder
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
Hi Spudhead,
Are you sure this is a proper way of doing? Because I googled and found a way from moving items from one list to another.
Here you go
Code:
$('select[name=List1] option:selected').appendTo('#List2');
Can u get a hint from the above code?
Thanx
11-03-2008, 10:27 AM
PM User |
#5
Senior Coder
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
It works, if it's like this:
Code:
<script type="text/javascript">
$(document).ready(function(){
$("#btn_AddToList").click(function(){
$('select[name=List1] option:selected').appendTo('#List2');
});
});
</script>
<select name="List1">
<option>---</option>
<option>Test1</option>
<option>Test2</option>
<option>Test3</option>
<option>Test4</option>
<option>Test5</option>
<option>Test6</option>
</select>
<input type="button" id="btn_AddToList" value="Add">
<select id="List2">
<option>---</option>
</select>
...but it seemed the
appendTo method is'nt able to append the text from the textbox using jQuery's
val() method:
Code:
<script type="text/javascript">
$(document).ready(function(){
$("#btn_AddToList").click(function(){
alert($('input[name=List1]').val()); // Let me know the textbox's value
$('input[name=List1]').val().appendTo('#List2');
});
});
</script>
<input name="List1" type="text">
<input type="button" id="btn_AddToList" value="Add">
<select id="List2">
<option>---</option>
</select>
Users who have thanked rangana for this post:
11-03-2008, 10:30 AM
PM User |
#6
Regular Coder
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
Thanx so much rangana for posting.
Would you recommend your method of doing this or Spudhead's method?
Thanx
11-03-2008, 10:46 AM
PM User |
#7
Senior Coder
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
I would recommend mine, as it's easy to modify and maintain,
Users who have thanked rangana for this post:
11-03-2008, 10:47 AM
PM User |
#8
Regular Coder
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
ok, thanx so much for your help.
God bless
11-03-2008, 11:00 AM
PM User |
#9
Regular Coder
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by
rangana
To insert new option on the listbox, use the
Option() object of JS, I'm not aware of any jQuery counterpart on that. This might help:
Code:
$(document).ready(function(){
$("#btn_AddToList").click(function(){
var opt=document.getElementById('lst_Regions');
var inp=$('input[name=txt_RegionName]').val();
opt.options[opt.options.length]=new Option(inp,inp);
});
});
While doing this, my firebug is showing the following error
opt is null
http://loopback/regions.php
Line 20
My line 20 is the following line:
opt.options[opt.options.length]=new Option(inp,inp);
When I
Code:
alert($('input[name=txt_RegionName]').val());
I can see the value in the alertbox.
Thanx
11-03-2008, 11:23 AM
PM User |
#10
Senior Coder
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
Could you show us your complete modification.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 05:06 AM .
Advertisement
Log in to turn off these ads.