1

I am new for this type of functionality, how can i retrieve the textbox values and that every value appending to the beside select box option after clicking on add button Any One please give me a suggestion to fixed it.

$(document).ready(function(){
var val = $('#txt-val').val();
var selectval = $('#newval').val();
  $('#addbtn').click(function(){
  //who to retrive textbox value and append as a option value of select box .
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" style="width:150px;" id="txt-val" />
<button id="addbtn">Add</button>
<select id="newval" style="width:150px;">
<option></option>
</select>

1 Answer 1

3

Just a line of code you need to add

Use .append() in your .click() method.

$(document).ready(function(){
  $('#addbtn').click(function(){
    var val = $.trim($('#txt-val').val());
    if(val != '')
      $('#newval').append('<option>' + val +'</option>');
    $('#txt-val').val('');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" style="width:150px;" id="txt-val" />
<button id="addbtn">Add</button>
<select id="newval" style="width:150px;">
</select>

Sign up to request clarification or add additional context in comments.

6 Comments

k nice but same value apending here repeatedly.
empty value should not append to select box how?
Ya Exactly Thanks
for the first option ( empty option ) not to appear , delete <option> from HTML
here its allowing spaces how to i override it as empty
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.