SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop-Down Menu Quirks

    I'm using the following coding in the respective portions of a site.

    Head:
    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function formhandler(form)
    {
    	var URL=document.websites.sites.options[document.websites.sites.selectedIndex].value;
    	window.location.href=URL;
    }
    //-->
    </script>
    Body:
    Code:
    <form name="websites" action="">
    <select name="sites" size="1" onchange="javascript:formhandler()">
    <option selected="selected">Sites:</option>
    <option value="#">-------------------</option>
    <option value="http://www.somesite.com/">Somesite</option>
    </select>
    </form>
    When the second option is selected ("-------------------"), there's a JavaScript error and the form ceases to function until the page is reloaded.

    I put the hash mark (#) in there to alleviate this problem, however it did not do the trick.

    Any ideas as to how to keep the script functioning when the dashes are selected?
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Drop-Down Menu Quirks

    Try this:
    Code:
    <form>
    <select size="1" onchange="formHandler(this)">
    <option selected="selected">Sites:</option>
    <option>-------------------</option>
    <option value="http://www.somesite.com/">Somesite</option>
    </select>
    </form>
    <script type="text/javascript">
    <!--
    function formHandler(selectObj)
    {
         var url = selectObj.options[selectObj.selectedIndex].value;
         if (url != '') location.href = url;
    }
    //-->
    </script>
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks much Anarchos. That works beautifully.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also pass the value itself and cut the function to 1 line:

    <select size="1" onchange="formHandler(this.options[this.selectedIndex].value)">
    ......
    function formHandler(optVal)
    {
    if (optVal != '') location.href = optVal;
    }

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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