0

I use below code to add options for dropdown

             //HTML CODE
            select(name='folist',  id='folist').
            //JS Code
             for(var i=0;i<data.foNameArray.length;i++){
            var combo = document.getElementById("folist");

            option = document.createElement("option");
            option.text = data.foNameArray[i];
            option.value =data.foIdArray[i];
             try {
                combo.add(option, null); //Standard 
            }catch(error) {
                combo.add(option); // IE only
            }
            }

It perfectly works.Now My doubt is how to select the value while adding like selected="selected".

2 Answers 2

2

You can set the selected property of option like this,

option.selected = true;
Sign up to request clarification or add additional context in comments.

Comments

2

you can use setAttribute to do that

option = document.createElement("option");
option.setAttribute("selected","selected");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.