I want to create a function in order to programmatically add some elements on a page.
Lets say I want to add a drop-down list with four options:
<
Here's an ES6 version, conversion to vanilla JS shouldn't be too hard but I already have jQuery anyways:
function select(options, selected) { return Object.entries(options).reduce((r, [k, v]) => r.append($('').val(k).text(v)), $('')).val(selected); } $('body').append(select({'option1': 'label 1', 'option2': 'label 2'}, 'option2'));