Hi,
I have a select box, is there a way to get javascript to select an item for me?
Here is my html, can I get javascript to select the option for ray?
<select name=“camplocation” id=“camplocation” >
<option value=“3”>–</option>
<option value=“18”>bridge</option>
<option value=“19”>can</option>
<option value=“20”>ray</option> </select></
thanks in advance
a few ways:
<script type="text/javascript">
window.onload=function(){
var s = document.getElementById('camplocation');
for( o=0; o<s.length; o++ ) {
if( s[o].value == '20' ) s[o].selected = true;
}
}
</script>
or
<script type="text/javascript">
window.onload=function(){
var s = document.getElementById('camplocation');
for( o=0; o<s.length; o++ ) {
if( s[o].innerHTML == 'ray' ) s[o].selected = true;
}
}
</script>
or
<script type="text/javascript">
window.onload=function(){
var s = document.getElementById('camplocation');
s[3].selected = true;
}
</script>
system
3
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script type="text/javascript">
window.onload=function(){
var sel = document.getElementById('camplocation');
for(i=0; i < sel.options.length; i++) {
if(sel.options[i].value == '20') {
sel.options[i].selected = true;
i = sel.options.length;
}
}
}
</script>
</head>
<body>
<select name="camplocation" id="camplocation" >
<option value="3">--</option>
<option value="18">bridge</option>
<option value="19">can</option>
<option value="20">ray</option>
</select>
</body>
</html>
Thanks Johnyboy, used your example 
Or, if you decide to use jQuery and make your Javascript life easier by 10000 times:
$("#camplocation option[value='20']").attr("selected", true);