How do I set the text values of a HTML drop down list to a value/variable from a external text or .js file. ( The value that will be displayed in the drop down list )
for example:
<select id="usernameDropDown">
<option value="username1">name1</option>
<option value="username2">name2</option>
<option value="username3">name3</option>
</select>
I'm trying to dynamically change the values as above "name1" and "name2" and so on to the values from a local .js file. Currently I have the values from the .js files written on the html page successfully with the following code segment:
<script src="jstest.js" type="text/javascript"></script>
<script Language='JavaScript'>
document.write(name1)
document.write(name2)
document.write(name3)
document.write(CSRUsername1)
document.write(CSRUsername2)
document.write(CSRUsername3)
</script>
currently the .js file is setup something like this:
var name1="Alpha"
var name2="Bravo"
var name3="Charlie"
var name4="Delta"
var name5="Echo"
Is there any method to set this text value using values from a text file or .js file? If possible, I would like to keep the solution in html / java script only..
How do I set the text values of a HTML drop down list to a value/variable from a external text or .js file. ( The value that will be displayed in the drop down list )
for example:
<select id="usernameDropDown">
<option value="username1">name1</option>
<option value="username2">name2</option>
<option value="username3">name3</option>
</select>
I'm trying to dynamically change the values as above "name1" and "name2" and so on to the values from a local .js file. Currently I have the values from the .js files written on the html page successfully with the following code segment:
<script src="jstest.js" type="text/javascript"></script>
<script Language='JavaScript'>
document.write(name1)
document.write(name2)
document.write(name3)
document.write(CSRUsername1)
document.write(CSRUsername2)
document.write(CSRUsername3)
</script>
currently the .js file is setup something like this:
var name1="Alpha"
var name2="Bravo"
var name3="Charlie"
var name4="Delta"
var name5="Echo"
Is there any method to set this text value using values from a text file or .js file? If possible, I would like to keep the solution in html / java script only..
Share Improve this question asked Mar 7, 2014 at 18:41 MitchbMitchb 1593 silver badges13 bronze badges 3- Maybe use something like Knockout.js? – Mike Christensen Commented Mar 7, 2014 at 18:43
- What causes the select value to change? – Mark Commented Mar 7, 2014 at 18:48
- @ Mike, looks like that would work unfortunately this needs to work on systems where I have to use already installed libraries... @ Mark, Nah can't use Jquery as above ^. – Mitchb Commented Mar 7, 2014 at 18:50
6 Answers
Reset to default 3Here's how to load a drop down dynamically [Fiddle].
HTML
<select id="usernameDropDown">
<option>Choose a user</option>
</select>
Javascript
var select = document.getElementById("usernameDropDown");
var unames = ["Alpha", "Bravo", "Charlie", "Delta", "Echo"];
for (var i = 0; i < unames.length; i++) {
var opt = unames[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
You can try setting the innerText
property of each of the option
elements:
var s = document.getElementById('names');
s.children[0].innerText = name1;
s.children[1].innerText = name2;
s.children[2].innerText = name3;
See this Fiddle.
Note that here you would need to know which child is where (e.g. the first child of the select
is name1
, etc.).
An alternative way to pick the children is to set ID attributes for each option, and use document.selectElementById
on each option
element.
Creates the options from scratch and adds them to the drop down list. The label is the text and the value is the value.
var dropdown = document.getElementById('usernameDropDown');
// if dropdown exists
if(dropdown) {
var usernames = [
{ label: 'name1', value: 'username1' },
{ label: 'name2', value: 'username2' },
{ label: 'name3', value: 'username3' }
];
for(var i=0,l=usernames.length; i<l; i++) {
var username = usernames[i];
var option = document.createElement('option');
option.setAttribute('value',username.value);
var optionText = document.createTextNode(username.label);
option.appendChild(optionText);
dropdown.appendChild(option);
}
}
My idea:
HTML:
<select id="usernameDropDown">
<option value="username1">name1</option>
<option value="username2">name2</option>
<option value="username3">name3</option>
</select>
Javascript:
window.onload = init;
var names=["Alpha", "Bravo", "Charlie", "Delta", "Echo"];
function init(){
var opts = document.getElementById("usernameDropDown").childNodes;
for (var i=0, i<opts.length, i++){
opts[i].innerHTML = names[i];
}
}
The javascript sets the 'innerHTML' property to the name string taken from the array. Not as elegant as Dissident Rage's version but shorter :D
Here is a really quick jQuery method that you could use
UPDATED version: It updates your select dynamically from your array fiddle
Your HTML
<select id="usernameDropDown">
<option value="username1">Username</option>
</select>
Your javascript
var name = ["Alpha","Bravo","Charlie","Delta","Echo"];
for(i=0;i<name.length;i++){
$("#usernameDropDown").append("<option value='name" + i + "'>" + name[i] + "</option>");
}
$("#usernameDropDown option").each(function(){
if($(this).html() == "name1")
{
$(this).html(name1)
}
if($(this).html() == "name2")
{
$(this).html(name2)
}
if($(this).html() == "name3")
{
$(this).html(name3)
}
});