jQuery: Textbox value to Listbox

  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

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: [ Select ]
<script>
           
           
    $(document).ready(function(){
   
   
        $("#btn_AddToList").click(function(){
        $('input[name=txt_RegionName]').appendTo("#lst_Regions");
         });
           
           
    });
</script>
  1. <script>
  2.            
  3.            
  4.     $(document).ready(function(){
  5.    
  6.    
  7.         $("#btn_AddToList").click(function(){
  8.         $('input[name=txt_RegionName]').appendTo("#lst_Regions");
  9.          });
  10.            
  11.            
  12.     });
  13. </script>



please tell me a solution.



ImageImage

Thanx
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

That's because "$('input[name=txt_RegionName]')" is targeting the actual element & "appendTo" appends the target element to the destination element.

You need to create a new <option> element using the value of the text box & append the <option> to the <select>.

Code: [ Select ]
    $('#but').click(function(){
        $('#list').append($("<option>" + $('#txt').val() + "</option>"));
        /*
            Or
            $("<option>" + $('#txt').val() + "</option>").appendTo($('#list'));
        */
    });
  1.     $('#but').click(function(){
  2.         $('#list').append($("<option>" + $('#txt').val() + "</option>"));
  3.         /*
  4.             Or
  5.             $("<option>" + $('#txt').val() + "</option>").appendTo($('#list'));
  6.         */
  7.     });
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

I was wondering... but couldn't really find anything on it. Can you call specific PHP functions with jQuery?

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 149 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.