1

I am dynamically setting the values for a select box using java script

var dropDwnObj = document.getElementById("workingBranchCodeId");
dropDwnObj.options.length = 0;
var anOption = document.createElement("OPTION");
dropDwnObj.options.add(anOption) ;

for(var i = 0; i < jsonResult.subLocationList.length; i++) {
  var anOption = document.createElement("OPTION");
  dropDwnObj.options.add(anOption) ;
  if(selValue == jsonResult.subLocationList[i].displayKey) {
    anOption.innerHTML = jsonResult.subLocationList[i].displayValue;
    anOption.value = jsonResult.subLocationList[i].displayKey;
  }
}

How can I make this value as the selected value for the select box? currently it is coming like an option but i want this option to be the selected one for the select box

3 Answers 3

1

Please set select box as multiple selection using multiple attriute like below

<select multiple>
</select>

and set selected attribute is true for options like below

anOption.selected = true;

Fiddle

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

1

dropDwnObj.options["your option"].selected = true;

Comments

0

anOption.selected = true;

worked for me

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.