As the question says, how do I add a new option to a DropDownList using jQuery?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Without using any extra plugins,
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.
——————————- Added ——————————–
DocumentFragmentis good option for speed enhancement, but we cannot create option element usingdocument.createElement('option')since IE6 and IE7 are not supporting it.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.
This way we’ll modify DOM for only one time!