0

This is the Select tag That I used..

<select class="form-control" id="VillagesComboidmod">
  <option>Kandy</option>
  <option>Ampitiya</option>
  <option>Semaneriya</option>
</select>

This is the code that I used to select a value..

document.getElementById('VillagesComboid').value = "Ampitiya";
1
  • use == document.getElementById('VillagesComboid').value == "Ampitiya"; Commented Mar 16, 2017 at 5:00

5 Answers 5

2

You've just got a typo in the reference to the element id.

To set the value of the <select> element

document.getElementById('VillagesComboid').value = "Ampitiya";

should be

document.getElementById('VillagesComboidmod').value = "Ampitiya";

Here it is working on JSBin

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.. :-) I just got it
Glad to hear it @SupunSasiduMadushankaSupun0 Cheers! :)
0

You should update your select like below:

<select class="form-control" id="VillagesComboidmod">
  <option value="Kandy">Kandy</option>
  <option value="Ampitiya">Ampitiya</option>
  <option value="Semaneriya">Semaneriya</option>
</select>

1 Comment

Thank you. I got it
0

Use, ==, for comparision. and = for assignment

document.getElementById('VillagesComboid').value == "Ampitiya";

<!DOCTYPE html>
<html>
<body>

<select class="form-control" id="VillagesComboidmod">
  <option>Kandy</option>
  <option>Ampitiya</option>
  <option>Semaneriya</option>
</select>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    alert(document.getElementById("VillagesComboidmod").value == "Ampitiya");
}
</script>

</body>
</html>

Please run the above CODE

Here is a working DEMO

1 Comment

@Supun Sasidu Madushanka Supun0, please check my answer and the example. you no need to add the value to options tag. just select a value and click on try it.
0

just change your id

document.getElementById('VillagesComboid').value='Ampitiya';

to

document.getElementById('VillagesComboidmod').value='Ampitiya';

Comments

0

Here you go

<html>
<body>

<p id="demo"></p>

<select class="form-control" id="VillagesComboidmod">
 <option value="K">Kandy</option>
 <option value="A">Ampitiya</option>
 <option value="S">Semaneriya</option>
</select>


<script>
  document.getElementById('VillagesComboidmod').value = 'A';
</script> 

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.