Jquery add options select list

Jquery add options select list

Posted: sslivigg Date: 21.07.2017

If you had lots of options, or this code needed to be run very frequently, then you should look into using a DocumentFragment instead of modifying the DOM many times unnecessarily.

For only a handful of options, I'd say it's not worth it though.

DocumentFragment is good option for speed enhancement, but we cannot create option element using document. What we can do is, create a new select element and then append all options. Once loop is finished, append it to actual DOM object. With no plug-ins, this can be easier without using as much jQuery, instead going slightly more old-school:. If you want to specify whether or not the option a is the default selected value, and b should be selected now, you can pass in two more parameters:.

You can do this:.

javascript - Adding options to a