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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts

    :: automatically making all select boxes go to default value ::

    can i do this as the page loads?

    <form name="est_projects">
    <select class="projectsmenus" name="FeaturedProjects" onChange="javascript:var projecturl=this.options[this.selectedIndex].value;
    document.location.href = projecturl;return true;">
    <option selected>:: Featured projects ::</option>
    <option value="http://www.google.com">Boston, Queen St</option>
    <option value="http://www.hotmail.com">Brighton, North Street</option>
    <option value="http://www.lycos.com">Darlington</option>
    <option value="http://www.excite.co.uk">Ely, Town Centre</option>
    </select>
    </form>

    im using that so far but if the user presses the back button all the menus lose their titles (the selected one)

    i have 3 of these on the page - any ideas?
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <body onload="document.est_projects.FeaturedProjects.options[0].selected=true;">
    <form name="est_projects">
    <select class="projectsmenus" name="FeaturedProjects" onChange="javascript: var projecturl=this.options[this.selectedIndex].value; document.location.href = projecturl;return true;">
    <option selected>:: Featured projects ::</option> 
    <option value="http://www.google.com">Boston, Queen St</option> 
    <option value="http://www.hotmail.com">Brighton, North Street</option> 
    <option value="http://www.lycos.com">Darlington</option> 
    <option value="http://www.excite.co.uk">Ely, Town Centre</option> 
    </select> 
    </form>
    </body>

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    thanks m8 - works a treat
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could also do something like this
    Code:
    function setSelects() {
    	var selects = document.getElementsByTagName('SELECT');
    	for (var i in selects)
    		selects[i].selectedIndex = 0;
    	}
    
    <body onLoad="setSelects();">
    also, your onChange event looks far too complicated. Why not this?
    Code:
    onChange="top.location.href=this.options[this.selectedIndex].value;"

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    None of those restore the default option ("selected"); they simply set the first as selected. This works for single selects:

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    function reset_selects() {
    var el, f = 0, e;
    while (frm = document.forms[f++]) {
    e = 0;
    while (el = frm.elements[e++]) {
    if (el.type == 'select-one') {
    for (var o=0; el.options[o]; ++o)
    if (el.options[o].defaultSelected) {
    el.selectedIndex = o;
    break;
    }
    if (o == el.options.length) el.selectedIndex = 0;
    }
    }
    }
    }

    onload = reset_selects;

    </script>
    </head>
    <body>
    <form>
    <select>
    <option>no default option</option>
    <option>option</option>
    <option>option</option>
    </select>
    <select>
    <option selected="selected">default option</option>
    <option>option</option>
    <option>option</option>
    </select>
    <select>
    <option>option</option>
    <option selected="selected">default option</option>
    <option>option</option>
    </select>
    <select>
    <option>option</option>
    <option>option</option>
    <option selected="selected">default option</option>
    </select>
    </form>
    </body>
    </html>
    Last edited by adios; 08-20-2002 at 11:50 PM.


  •  

    Posting Permissions

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