View Full Version : dropdown value from javascript.

09-25-2012, 02:09 PM
I have a value receiving from database and saved it to a Javascript variable. I would like to pass this as an option to dropdown. How to achieve this.

I tried below code that didn't work. ( I want the name "Country" in dropdown option).

window.onload = hello;
function hello()
var dd = document.getElementById("dropdownID");
dd.options[dd.selectedIndex].value = "Country";


below is dropdown.

<select id="dropdownID" onchange="hello()">
<option value="one"> </option>

09-25-2012, 02:34 PM
This works perfectly ....

But maybe you didn't want to change the value attribute but rather the visibile text?

dd.options[dd.selectedIndex].innerHTML = "Country";

09-25-2012, 02:41 PM

dd.options[dd.selectedIndex].text = "Country";

09-25-2012, 02:51 PM
Do you want to change the text of the only option in the select? Or did you want to add an option, giving the select two options?

09-25-2012, 03:55 PM
I want to add only one option. devnull69 and xelawho answered it correctly. thanks you all. However, it is only taking the text and not the value (i am speaking about <option value="country">). Tried the below code but not effect. Let me know if I am making anything wrong. This is because, if I submit again, it is taking blank values.

dd.options[dd.selectedIndex].text = "Country";

09-25-2012, 04:00 PM
they're two separate attributes, so you would have to do something like this:

dd.options[dd.selectedIndex].text = "Country";
dd.options[dd.selectedIndex].value = "Country";