0
<select name="ethinicity" >
            <option value="type1">Asian</option>
            <option value="type2">African</option>
            <option value="type3">Caucasion</option>
            <option value="type4">Hispanics</option>
            <option value="type5">Other Populated Groups</option>
</select>

I am using

document.getELementsByname("ethinicity").value="type2"

to change its value,but it donot work in that way,why i can't do like this? and give me other way to do this but i donot want to put this in form and then changing it like

document.formName.ethinicity.value="type2"

Thanks in advance!

sony
  • 375
  • 1
  • 6
  • 21
  • It is not duplicate i already checked all of them, but cant find any single answer which is specific to my question. – sony Jul 23 '15 at 21:12
  • you need to change the `selectedIndex` of your `select`...This is a duplicate. – brso05 Jul 23 '15 at 21:14

2 Answers2

1

You can do this:

document.getElementsByName("ethinicity")[0].selectedIndex = 1;

You should give your select an id like this:

<select id="ethinicity" name="ethinicity" >

document.getElementById("ethinicity").selectedIndex = 1;
brso05
  • 13,142
  • 2
  • 21
  • 40
1

You need to pass the name of the elements as a parameter to the function you are using to select them alongwith first array index:

document.getELementsByName("ethinicity")[0].value="type2"
sony
  • 375
  • 1
  • 6
  • 21
taxicala
  • 21,408
  • 7
  • 37
  • 66