Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-03-2008, 06:33 AM   PM User | #1
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Question 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
Attached Thumbnails
Click image for larger version

Name:	Administrator-Control-Panel.png
Views:	1688
Size:	1.9 KB
ID:	6828  
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-03-2008, 09:14 AM   PM User | #2
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
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);
	});			
});
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Users who have thanked rangana for this post:
cancer10 (11-03-2008)
Old 11-03-2008, 09:34 AM   PM User | #3
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
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
Spudhead is offline   Reply With Quote
Users who have thanked Spudhead for this post:
cancer10 (11-03-2008)
Old 11-03-2008, 09:43 AM   PM User | #4
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
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
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-03-2008, 10:27 AM   PM User | #5
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
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>
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Users who have thanked rangana for this post:
cancer10 (11-03-2008)
Old 11-03-2008, 10:30 AM   PM User | #6
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Thanx so much rangana for posting.

Would you recommend your method of doing this or Spudhead's method?


Thanx
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-03-2008, 10:46 AM   PM User | #7
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
I would recommend mine, as it's easy to modify and maintain,
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Users who have thanked rangana for this post:
cancer10 (11-03-2008)
Old 11-03-2008, 10:47 AM   PM User | #8
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
ok, thanx so much for your help.

God bless
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-03-2008, 11:00 AM   PM User | #9
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 795
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Exclamation

Quote:
Originally Posted by rangana View Post
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
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-03-2008, 11:23 AM   PM User | #10
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Could you show us your complete modification.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:06 AM.


Advertisement
Log in to turn off these ads.