21

How to add item to a listBox using jquery.

for example in the following listbox

<option value="1"></option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="0">All</option>

1
  • There are no listboxes in HTML. Do you mean a <select>? Commented Oct 4, 2010 at 9:54

2 Answers 2

32
$('#Select1').append('<option value="5">item 5</option>'); // adds item 5 at the end
$('#Select1 option').eq(0).after('<option value="new">new</option>'); // add new at the second position.
Sign up to request clarification or add additional context in comments.

Comments

11

Several methods are there. You can use

$('<option value="5">item 5</option>').appendTo('#listbox');

$('#listbox').append('<option value="5">item 5</option>');

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.