Javascript dynamically add options to select box

Can you give me some pointers? And, finally after quite a delay The one thing I'd avoid is doing DOM operations in a loop to avoid repeated re-renderings of the page.

I don't recommend doing DOM manipulations inside a loop -- that can get expensive in large datasets. Instead, I would do something like this:. You can read more about DocumentFragment on MDN , but here's the gist of it:. It is used as a light-weight version of Document to store a segment of a document structure comprised of nodes just like a standard document. The key difference is that because the document fragment isn't part of the actual DOM's structure, changes made to the fragment don't affect the document, cause reflow, or incur any performance impact that can occur when changes are made.

What is the best way to add options to a select from an array with jQuery? None of the above solutions worked for me. Append method didn't give error when i tried but it didn't solve my problem.

In the end i solved my problem with data property of select2. I used json and got the array and then give it in select2 element initialize.

Changing Select element content on the fly

For more detail you can see my answer at below post. By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users.

Sign up or log in to customize your list.

Javascript Add/Remove More Form Fields

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7.

javascript - Add more options to select boxes in dynamically created table - Stack Overflow

Join them; it only takes a minute: Adding options to select with javascript Ask Question. Voting to reopen, as the linked 'duplicate' only has jQuery-based answers, whereas this one requires or at least implies a requirement of plain JavaScript. You could achieve this with a simple for loop: Edited in response to comment from OP: David Thomas k 29 Also, extending host prototypes while tempting is considered a bad idea. I think this should be updated to use innerText instead of innerHTML to avoid cross-site scripting vulnerabilities.

What do you think DavidThomas? Since you want to reuse this code, here's the function for it: Not sure I follow - you want to add the same options to more than one select element?

javascript dynamically add options to select box

I factored it into a function you can call where you just pass in the id of the select element you want to append the options to. That's what documentFragment is made for. You can easily modify the accepted answer so that options would be appended to documentFragment at first, preventing multiple renderings.

Javascript - add select programmatically - Stack Overflow

Instead, I would do something like this: Sign up or log in StackExchange. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name.

javascript dynamically add options to select box

Stack Overflow works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,4 stars - 429 reviews
inserted by FC2 system