SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple drop-down menus

    Hello all!
    I want to have a page with multiple drop-down menus. The user will select for example "type of car" and a drop-down menu of 3-4 colors will appear... And then, if the user selects one of these colors, another drop-down menu will appear with the available brands of cars for the respective colour...
    Can you give me hints on how to start?

  2. #2
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    Or this

    Dynamic drive is a good resource site for what you are after
    Regards,
    BJ Duncan

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BJ Duncan View Post
    Try this

    Or this

    Dynamic drive is a good resource site for what you are after
    The forums are crammed with questions about getting their scripts to work properly - or more than once on a page.

    "Dynamic drive - a place to look but not to touch."
    Tab-indentation is a crime against humanity.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ktsirig View Post
    Hello all!
    I want to have a page with multiple drop-down menus. The user will select for example "type of car" and a drop-down menu of 3-4 colors will appear... And then, if the user selects one of these colors, another drop-down menu will appear with the available brands of cars for the respective colour...
    Can you give me hints on how to start?
    Get to grips with SelectCascade. If you have any queries you can ask here.
    Tab-indentation is a crime against humanity.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Before we get scripting, let's first come up with the html so that it's workable without it.

    Code html4strict:
    <form>
        <p>
            <label for="cartype">Type of car</label>
            <select id="cartype">
                <option value="">Choose</option>
                <option value="2door">2 door</option>
                <option value="4door">4 door</option>
                <option value="5door">5 door</option>
            </select>
        </p>
        <p>
            <label for="carcolour">Colour of car</label>
    	<select id="carcolour">
                <option value="">Choose</option>
                <option value="red">Red</option>
                <option value="green">Green</option>
                <option value="blue">Blue</option>
                <option value="white">White</option>
            </select>
        </p>
        <p>
            <label for="carbrand">Brand of car</label>
            <select id="carbrand">
                <option value="">Choose</option>
                <option value="Ford">Ford</option>
                <option value="Nissan">Nissan</option>
                <option value="Toyota">Toyota</option>
                <option value="Honda">Honda</option>
            </select>
        </p>
    </form>

    Now with the script, we want the car colour and car brand to be hidden when the page loads. This script is going to be placed at the end of the page, just before the body, so that the page can be modified without resorting to onload tactics.

    The different car elements are going to placed into a single car object, to make accessing them easier and more consistant. because the label and select elements are nested, we can use the parentNode of the select element to easily hide them.

    Code javascript:
    var car = {
    	type: document.getElementById('cartype'),
    	colour: document.getElementById('carcolour'),
    	brand: document.getElementById('carbrand')
    };
    car.colour.parentNode.style.display = 'none';
    car.brand.parentNode.style.display = 'none';

    When the car type and car colour is greater than 0, we can show the next select element

    Code javascript:
    function setVisibility(el, visible) {
    	console.log(el);
    	el.style.display = visible ? '' : 'none';
    }
    car.type.onchange = function () {
    	setVisibility(car.colour.parentNode, this.selectedIndex);
    }
    car.colour.onchange = function () {
    	setVisibility(car.brand.parentNode, this.selectedIndex);
    }

    And that's all that's needed.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •