Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an option to a dropdown list using jQuery?

I'm trying to use the following code to add an option to a dropdown list in ASP.NET. Any ideas why this doesn't work? I tried Googling but can't figure out why this won't work.

What shoud the code do? I have an ASP.NET dropdown list. I want to access the dropdown list by name and add an item to the list. The item should have descriptive text of "Some Text" and a value of "123".

Thanks!

$("#ddlCategory").append($("<option>Some Text</option>").val(1).html("123"));
like image 806
DenaliHardtail Avatar asked Nov 03 '11 15:11

DenaliHardtail


People also ask

How do I add options to a Dropdownlist using jQuery?

Method 1: Append the option tag to the select box The option to be added is created like a normal HTML string. The select box is selected with the jQuery selector and this option is added with the append() method. The append() method inserts the specified content as the last child of the jQuery collection.

How do I append a dropdown option?

Select add() Method The add() method is used to add an option to a drop-down list. Tip: To remove an option from a drop-down list, use the remove() method.

How can we create dynamic drop-down list in html using jQuery?

To add a dropdown list dynamically, you would need to create the HTML <select> element, its label and optionally a <br> tag. In pure JavaScript, you can use the document. createElement() method to programmatically create a dropdown list. Then you can call the Node's appendChild() method or jQuery's .


1 Answers

var newOption = "<option value='"+"1"+"'>Some Text</option>"; 
$("#ddlCategory").append(newOption);
like image 83
Mark Schultheiss Avatar answered Oct 06 '22 00:10

Mark Schultheiss