5

I have a issue with populating values in a box using JQuery.

Instead of adding it underneath each other it adds it next to all other elements

my code

$('#pages option').append($('#t1').val());

3 Answers 3

12

I think you want

$('#pages').append($('#t1').val());

assuming pages is the id of your <select>. Also, $('#t1').val() should be an <option> element, not a value. Something like this

 var newOption = $('<option value="'+val+'">'+val+'</option>');
 $('#pages').append(newOption);

or

var newOption = $('<option>');
newOption.attr('value',val).text(val);
 $('#pages').append(newOption);

whichever is easier for you to read.

Here's a Working Demo

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

3 Comments

Yes it is the id of the select
Your code works but it's adding the element, but I'm not able to selected that element
The value does not get an option tag eg value and not <option>value</option>
2

You probably want something along the lines of

$("#pages").append("<option>"+$("#t1").val()+"</option>");

That will make and append an option to your select box

Comments

1

Try this..

using jquery you can use

  this.$('select#myid').append('<option>newvalue</option>');

where "myid" is the id of the dropdown list and newvalue is the text that you want to insert..

Comments

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.