Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    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 Attached Thumbnails jQuery: Textbox value to Listbox-administrator-control-panel.png  

  • #2
    Senior Coder rangana's Avatar
    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);
    	});			
    });
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    phantom007 (11-03-2008)

  • #3
    Senior Coder Spudhead's Avatar
    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:

    phantom007 (11-03-2008)

  • #4
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 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

  • #5
    Senior Coder rangana's Avatar
    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>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    phantom007 (11-03-2008)

  • #6
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Thanx so much rangana for posting.

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


    Thanx

  • #7
    Senior Coder rangana's Avatar
    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,
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    phantom007 (11-03-2008)

  • #8
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    ok, thanx so much for your help.

    God bless

  • #9
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    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

  • #10
    Senior Coder rangana's Avatar
    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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •