Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: add an <option> to a <select>

When I attempt to add an option to a select, the option I’m trying to append ends up appended to the first option instead of the select itself.

$(".ct [value='']").each(function() {
    $(this).append($("<option></option>").attr("value", "Reset").text("Reset"));
});

Help?

like image 610
user135498 Avatar asked Oct 05 '09 05:10

user135498


People also ask

How to add option in select jQuery?

Method 1: Append the option tag to the select box 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. Hence the option is added to the select element.

How add and remove select option in jQuery?

The remove() method removes the options, the end() takes the object back to the beginning of the script, and the append() method adds new options in the list.

How can I set the value of a DropDownList using jQuery?

Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.

How set multiple selected values in DropDownList jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


1 Answers

I'd have to see the HTML you're targetting to be sure, but it looks to me like your selector is targetting the first option of the select.

Try $(this).parent.append() instead.

like image 147
Jeff Avatar answered Sep 21 '22 08:09

Jeff