Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2003
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default value in dropdown menu

    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>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);
    }
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    New Coder
    Join Date
    Jan 2003
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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??

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    document.forms[formname].[selectname].selectedIndex = 0;
    Worth a shot, yes?

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sel.options[0].selected = true;
    Last edited by brothercake; 04-02-2003 at 10:13 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <!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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •