2

I'm trying to program a website using HTML and I have a question. I'm using a drop-down list with options A and B, but after one of this is chosen, I'd like to create another drop down list and text fields at the same page, with different questions. For example, If some one choose A, then it'll be asked questions C and D, and if someone chose B, it'll be asked questions E and F. What is the best way to program this ?

Sorry for any mistake in English, I'm not a native speaker. And Thanks in advance.

Marcelo
  • 1,503
  • 2
  • 15
  • 16
  • You will need javascript for that. The answer *might* be a bit complex though. – yoda Dec 23 '10 at 19:54
  • 1
    You can check http://stackoverflow.com/questions/3637972/whats-the-best-and-easiest-way-to-populate-a-dropdown-based-on-another-dropdown if there is dynamic data (fetched from database) involved. Check my answer to the same question http://stackoverflow.com/questions/3637972/whats-the-best-and-easiest-way-to-populate-a-dropdown-based-on-another-dropdown/3638893#3638893 – Sandeepan Nath Dec 23 '10 at 19:55
  • Hi, I'm a beginner and I'm using simple basic stuffs like – Marcelo Dec 23 '10 at 19:56
  • These "stuffs" are html tags. As it is said before you'll need javascript. I recommend using jquery library (http://jquery.com). It will make things shorter and easier. – Javi Dec 23 '10 at 20:00

4 Answers4

5

It depends on whether the data is static (i.e. coded on the page and will not change) or dynamic (i.e. delivered from the web server).

If your drop-down lists are supposed to only filter their contents based on List A, then you will probably want to use some JavaScript to hide / remove the items that you don't want. Consider looking at jQuery for this as it's an extremely easy to use and understand framework and there are plenty of good examples both on Stack Overflow and throughout the Internet that you can refer to.

If your data needs to be delivered from a web server (i.e. you have a database that you're referring to for the list values) then you'll need to provide more information about your programming framework (e.g. ASP.Net, PHP, etc) and how you want the lists to work (Ajax, full-page postback, etc).

Let's assume you're keeping everything static on the page and hiding information based on the user's selection.

Your HTML might look something like this:

<body>
    <div id="myQuestions">
        <select id="QuestionOptions">
            <option value="A">Question A</option>
            <option value="B">Question B</option>
        </select>
    </div>
    <div id="myAnswers">
        <div id="A" style="display: none;">
            <div id="QuestionC">
                <p>Here is an example question C.</p>
            </div>
            <div id="QuestionD">
                <select id="QuestionOptionsD">
                    <option value="G">Question G</option>
                    <option value="H">Question H</option>
                </select>
            </div>
        </div>
        <div id="B" style="display: none;">
            <div id="QuestionE">
                <p>Here is an example question E.</p>
            </div>
            <div id="QuestionF">
                <select id="QuestionOptionsF">
                    <option value="I">Question I</option>
                    <option value="J">Question J</option>
                </select>
            </div>
        </div>
    </div>
</body>

Your JavaScript (I'm using jQuery, as mentioned above) could then look like this:

$(function () {
    $('#QuestionOptions').change(function () {
        $('#myAnswers > div').hide();
        $('#myAnswers').find('#' + $(this).val()).show();
    });
});
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • Hi, thanks for you answer, so for the function I should create inside the part ? Thanks in advance. – Marcelo Dec 23 '10 at 20:41
  • Phil, very clean and elegant answer. @Marcelo - yes you have to put that inside a script tag and of course include the jQuery JS file before it. – Michiel Kalkman Dec 23 '10 at 21:03
1

You could do it using javascript appendChild() (Example Here) and append the div dynamically based on which option is chosen. Or you could have the other selects static and have them hidden with css, then display which one you want depeding on which option is chosen.

Will
  • 19,661
  • 7
  • 47
  • 48
0

Have a hidden dropdown that corresponds to each option in the first dropdown. Use the onclick handler on the first dropdown to change the corresponding hidden dropdown's hidden attribute.

jQuery might make your life easier on this one.

Chetan
  • 46,743
  • 31
  • 106
  • 145
0

I might not understand your question, but it sounds like you might be overcomplicating things. If you are simply trying to prompt two separate sets of questions you don't need javascript, you can just have two separate pages with the different questions. A -> links to a page w/ questions C/D while B-> links to E/F.

You might try posting some code and even a demo for interface questions... it's a very good way to help articulate your question.

Derek Adair
  • 21,846
  • 31
  • 97
  • 134