views:

58

answers:

4

I have a SELECT which looks like this.

<select id="mySelect"><option value="">Please select</option></select>

At a certain point, my javascript sets the OPTION as follows:

var elSel = document.getElementById('mySelect');
elSel.options[0].value = myValue;
elSel.options[0].text = myText;

The problem is that you have to click the select for it to show myText. How do I make it so that myText (with myValue) shows as soon as I run that javascript?

A: 

Load the script with onLoad attribute of the body tag? e.g.

<body onload="initSelect()">

Or simply put the script after the select tag:

<select>...</select>
<script>//code to generate the options</script>
William
The problem is that the JS is being executed when they click a button. So it can't happen onload, and it can't always happen as in your second example. Sorry for not specifying this earlier.
babonk
+1  A: 

Add elSel.selectedIndex = 0; to the end of your script. Use elSel.options.length-1 if you're going to ever have more than 1 item and you want to select the last item.

<html>
<head>
<script language="javascript">
function addItem() {
var elSel = document.getElementById('test');
elSel.options[0].value = '1';
elSel.options[0].text = 'new value';
elSel.selectedIndex = 0;
}
</script>
</head>
<body>
<form>
<select id="test"><option value="1">- SELECT AN ITEM -</option></select>
<input type="button" value="Add Item" onclick="addItem();" />
</form>
</body>
</html>
Tahbaza
tried that, didn't work. (using Firefox 3.0.11)
babonk
actually, it does work just fine. I've posted the entire code example for you to try, also tested in Chrome and the latest version of Firefox.
Tahbaza
interesting, its not working for me. something I should add is that it's disabled (because of the css) prior to having the element added.however i find that putting the .disabled = false before the option adding code vs after it makes no difference.
babonk
Why not see if the posted code works for you; if it does it must be some other interaction on your page that you can then focus on finding.
Tahbaza
Tahbaza: Your code did work, and I narrowed it down to a problem in a jquery plugin I was using. Thanks
babonk
A: 

Try using DOM manipulation:

<select id="mySelect">
<option value="">Please select</option>
</select>
<script>

var elSel = document.getElementById('mySelect');

var opt = {};


opt = document.createElement('option');
opt.value = '1';
opt.text = 'a';
elSel.appendChild(opt);

opt = document.createElement('option');
opt.value = '2';
opt.text = 'b';
opt.selected = true;       /* Here is where we update the select element */
elSel.appendChild(opt);

opt = document.createElement('option');
opt.value = '3';
opt.text = 'c';
elSel.appendChild(opt);

</script>

Test it here:

http://dl.dropbox.com/u/186012/demos/stackoverflow/select/index.html

Sebastián Grignoli
A: 

The problem was the jQuery plugin I was using (Uniform), I didn't realize that I had to run $.uniform.update()

http://pixelmatrixdesign.com/uniform/

babonk