Javascript change dropdown selected value

Hi Guys,

I need to change the value of a dropdown select box and am using the following:

window.document.managedomains.getElementById(["category["+domainid+"]"]).value = 'This is the selected message!!!';

However, the above code does not work. Any ideas how to convert it so that it will function properly?

You have 2 different cases.

  1. adding a new value:
var dd=document.getElementById("category")
var aryOptions=dd.getElementsByTagName('option');
var opt=document.createElement('option');
opt.appendChild(document.createTextNode("This is the selected message!!!"));
  1. finding an existing value:
var dd=document.getElementById("category")
var aryOptions=dd.getElementsByTagName('option');
var cpt=0;
var indexValue=false;
  if(aryOptions[cpt].textContent=='This is the selected message!!!'){

The select element contains a nodeList called options, that references each individual option.
The select element also contains a property called selectedIndex, which holds the number of the currently selected option.

Put these together and you get something that goes a little like this:

var select = window.document.managedomains.getElementById(["category["+domainid+"]"]);
select.options[select.selectedIndex].value = 'This is the selected message!!!';

Still can’t get it to work. I think the problem lies within this line:

var select = window.document.managedomains.getElementById(["category["+domainid+"]"]);

“managedomains” is the name of my form. “category[id]” is the name of the select menu. Does something need to be changed?

I apologise for the questions but javascript is not my speciality :slight_smile:

try to simply do a


drop the window, and forget your form.
An ID must be unique in the page, and the getElementById() scans all the document, if you don’t give a DOM element as the root, so your field will be found.
And remove the brackets in your getElementById(), it takes a string as argument, not an array.

Take my precedent example, and derive it.
It’s been tested, and it works.


Thanks for your reply. I think I need to use window because the destination form lies within a different window (parent). Please could you confirm?

The following should have all the answers you need for cross-window scripting.

So, in theory, this should work right?

var form = window.document.getElementById('managedomains');
	var select = form["category["+domainid+"]";
	select.options[select.selectedIndex].value = "This is the selected message!!!";

However, I just tried it and it’s still not working…weird :frowning: