Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change the selected item

Status
Not open for further replies.

JamesManke

Programmer
Jul 1, 2004
54
CA
Is there a way to have javascript change the selected item in
a select box without refreshing the page.

Thanks
 
JamesManke,

Sure, selectedIndex property is read/write. This is a demo.
Code:
<html>
<head><title>media types</title>
<script language="javascript">
function doit() {
    var oelem=document.getElementById("mediatype");
    oelem.selectedIndex=parseInt(Math.random()*(oelem.length-1))+1;
}
</script>
</head>
<body>
<form>
<select id="mediatype" name="mediatype">
    <option value="empty">Choose Media Type</option>
	<option value="Text/xml">Text/xml</option>
	<option value="Application/xml">Application/xml</option>
    <option value="Text/xml-external-parsed-entity">Text/xml-external-parsed-entity</option>
    <option value="Application/xml-external-parsed-entity">Application/xml-external-parsed-entity</option>
    <option value="Application/xml-dtd">Application/xml-dtd</option>
</select>
</form>
<hr />
<button name="btntest" onclick="doit()">random change Media Type selection</button>
</body>
</html>
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top