...

View Full Version : Default value in dropdown menu



CoraLover
04-02-2003, 10:01 AM
How do get the dropdown menu to return to its default value after the user selects an item from it?? Btw, the dropdown menu is dynamic, whereby all the items are retrieved from the database using JSP.

For instance,
<SELECT NAME="cbo">
<OPTION VALUE="Default">DEFAULT VALUE</OPTION>
<OPTION VALUE="1">Small</OPTION>
<OPTION VALUE="2">Medium</OPTION>
<OPTION VALUE="3">Large</OPTION>
</SELECT>

brothercake
04-02-2003, 10:27 AM
you can use onchange to read a select value

<select onchange="getValue(this)">

then

function getValue(sel)
{
var selValue= sel.options[sel.options.selectedIndex].value;
alert(selValue);
}

CoraLover
04-02-2003, 10:31 AM
Ok, thanks, brothercake!! But how i can i set the selected dropdown menu to its default value? How to get the dropdown menu to return to index 0 after the user selects an item from it??

Choopernickel
04-02-2003, 02:22 PM
document.forms[formname].[selectname].selectedIndex = 0;


Worth a shot, yes?

brothercake
04-02-2003, 10:11 PM
sel.options[0].selected = true;

cheesebagpipe
04-02-2003, 10:45 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function showOptionValue(oSelect) {
status = oSelect[oSelect.selectedIndex].text; //gets displayed text, shows in status line
status += ' : ';
status += oSelect[oSelect.selectedIndex].value; //gets value
}

function resetSelect(oSelect) {
for (var i=0; i<oSelect.options.length; ++i) //loop through select options
if (oSelect.options[i].defaultSelected) //this option's defaultSelected property is set to true (from HTML: selected="selected")
oSelect.options[i].selected = true; //so, set its selected property to true, selecting it
showOptionValue(oSelect); //reset status
}

</script>
</head>
<body>
<form>
<!-- call handler function, pass Select object (represents drop-down list) -->
<SELECT NAME="cbo" onchange="showOptionValue(this)">
<OPTION VALUE="not_default">Not DEFAULT VALUE</OPTION>
<OPTION VALUE="1">Small</OPTION>
<!-- default value, preselected -->
<OPTION VALUE="2" selected="selected">Medium</OPTION>
<OPTION VALUE="3">Large</OPTION>
</SELECT>
<!-- call handler function, pass Select object using its name as a variable -->
<input type="button" value="Reset Drop-down" onclick="resetSelect(cbo)">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum