How to make drop down option selected in javascript

Js

Posted on 18 Dec 15 by Vickey
  955



With the help of below javascript code we can select a dropdown option dynamically.I have a dropdown box with id exp_year

<select id="exp_year">

<option value="2001"></option>

<option value="2002"></option>

<option value="2003"></option>

<option value="2004"></option>

<option value="2005"></option>

</scelect>
var exp_year = document.getElementById("exp_year");

for (var i = 0; i < exp_year.options.length; i++) {

	if (exp_year.options[i].value== 2004) {
	
		exp_year.options[i].selected = true;
		
		break;
	 
	}
}

 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com