SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast blackbird52's Avatar
    Join Date
    Nov 2001
    Location
    Bloomington, IN
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Auto-Selecting a Drop-Down Menu

    HTML Code:
    [color=black]<form action="" method="post" name="form1"> 
    
    Here is a function I co-wrote that selects an option in a drop-down menu based on a value you pass from another page. Here I needed to have a company contact page that automatically selected a department, depending on which department page you were coming from. You have to add a value to the end of the link to the contact page, making it different for each department.
     
    FUNCTION:
    
    Code:
    <SCRIPT LANGUAGE=javascript>
    <!--
    function SetList() {
    var dept = ""
    var num
    var len
    num = window.location.search
    len = num.length
    dept = num.charAt(len-1)
    if (dept == null) {
    dept = 0;
    } else {
    (document.form1.emailto.selectedIndex = dept);
    }
    }
    Code:
    //-->
    </script>
    Add an onload statement to the body tag: onLoad="SetList();" FORM CODE:
    [color=black]<select class="FORM" name="emailto">[/color] [color=black]<option VALUE="">Department 1</option>[/color] [color=black]<option VALUE="">Department 2</option>[/color] [color=black]<option VALUE="">Department 3</option>[/color] [color=black]</select>[/color] [color=black]</form>[/color]

    Here form1 is the name of your form and emailto is the name of your drop-down menu, or select menu.

    HTML Code:
    [color=black]<a href="contact.html?dept=1">Contact Department 2</a>Then, when linking to your contact page, you add a number on the end of the link like this. Note that the dept= is not needed, all the script is doing is pulling the number off of the end of the URL. This script only looks for a one digit number, so you can't have more than 10 options (0-9).
    [/color]
    The options in a select menu can be called with numeric values, starting with 0 for the first one and continuing from there. So the value I am using above actually causes the select menu to display the 2nd option.

    I am not sure this is the best way to do this, but it is a nice way to avoid having to setup a database.

    Hope this is usefull for someone.
    [/color]

  2. #2
    SitePoint Evangelist Azam.net's Avatar
    Join Date
    Apr 2002
    Location
    UK
    Posts
    479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wow, this is BRILLIANT! Thanks a million.

    Planning to ask people what country they come from on home page of my new site. The info can then be reused using your technique.

    P.S. You got an example of anywhere you've used the code?
    Azam Marketing, Inc.
    Award-Winning Digital Marketing and Design Results Since 1997
    Click here to read acclaimed online marketing & design blog

  3. #3
    SitePoint Enthusiast blackbird52's Avatar
    Join Date
    Nov 2001
    Location
    Bloomington, IN
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Example

    seubert.com

    I used the script on this site. On the bottom-right of each of the four tabs there is a contact link.


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
  •