user135498
user135498

Reputation: 6133

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?

Upvotes: 16

Views: 26767

Answers (2)

meder omuraliev
meder omuraliev

Reputation: 186562

  $(this).parent().append($("<option></option>").attr("value", "Reset").text("Reset"));

Upvotes: 1

Jeff
Jeff

Reputation: 2871

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.

Upvotes: 7

Related Questions