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?
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.
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.
Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.
With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With