groenroos / minimalect

Minimal select replacement for jQuery
350 stars 52 forks source link

Dynamically adding <option>s to the select element seems buggy #35

Closed jonobobo closed 10 years ago

jonobobo commented 11 years ago
$("#myselect optgroup").eq(0).append('<option value="Foo">Bar</option>');
$("#myselect").change();

^^ Successfully adds an in a selectbox. This also updates the Minimalect dropdown, but only the FIRST TIME.

Subsequent calls (with different option value/text pairs) just don't work. IE, the option just doesn't append in Minimalect.

What's the recommended procedure for adding/removing/updating options in a select that's being bound to Minimalect, or is this just a bug?

jonobobo commented 11 years ago

Here's a jsfiddle: http://jsfiddle.net/HPwGC/

Note the first call to update the selectbox works correctly in Minimalect, but subsequent calls are ignored, despite calling .change()

jonobobo commented 11 years ago

Ok this can be circumvented by using .val() before .change(), although I still feel it's a little awkward. Changing the options within a selectbox should automatically render to Minimalect.

Working code to dynamically add options to the selectbox:

$("#myselect").append('<option value="foo">bar</option>').val("foo").change();

Just using val() without the value parameter also works.

groenroos commented 10 years ago

Just a heads up; 5bb4226 adds much better dynamic change detection via MutationObserver (polyfill or public method callback required for IE and older others) -- might wanna check it out!

jonobobo commented 10 years ago

Ah great stuff. Thanks for all your hard work