JSFiddle - React, Tailwind, and code Playground
by rwaldron
HTML
<select name="foo">
<option value="alpha">alpha</option>
</select>
JavaScript
function appendOption( parent, val ) {
var $select = $('select[name="' + parent + '"]'),
$options = $select.children('[value="' + val + '"]');
if ( !$options.length ) {
$("<option/>", {
value: val,
text: val
}).appendTo( 'select[name="' + parent + '"]' );
}
}
var options = [ "alpha", "beta", "gamma", "gamma", "beta" ];
$.each( options, function( idx, opt ) {
appendOption("foo", opt);
});
// Assertions
console.log(
$('select[name="foo"]').children().length === 3 &&
"Pass: There are 3 options" || "Fail"
);