2

I'm trying to make a a webpage that has a conditional dropdown box using html and javascript. Say it's a certain day in the week like Monday I need a different dropdown box with different options when it's Thursday; however some days may have the same options. Originally I had two drop down box one when the person clicked what day it was and then the dropdown box would appear, but I was wondering if there was a way to do this automatically using a javascript function and also creating a function that only displays a certain dropdown list.

<html>
 <head></head>
 <body>
    <div id="myQuestions">
        <select id="QuestionOptions">
            <option value disalbe selected>What Day is it?</option>
            <option value="A">Monday</option>
            <option value="A">Tuesday</option>
            <option value="B">Wednesday</option>
            <option value="B">Thursday</option>
            <option value="B">Friday</option>
            <option value="A">Saturday</option>
            <optoin value="A">Sunday</optoin>
        </select>
    </div>
    <div id="myAnswers">
        <div id="A" style="display: none;">
            <div id="QuestionC">
                <p>Exapmle of question.</p>
            </div>
            <div id="QuestionD">
                <select id="QuestionOptionsD">
                    <option value disalbe selected>Choose Your Answer</option>
                    <option value="G">Answer G</option>
                    <option value="H">Answer H</option>
                </select>
            </div>
        </div>
        <div id="B" style="display: none;">
            <div id="QuestionE">
                <p>Exmaple of another question.</p>
            </div>
            <div id="QuestionF">
                <select id="QuestionOptionsF">
                    <option value disalbe selected>Choose Your Answer</option>
                    <option value="I">Answer I</option>
                    <option value="J">Answer J</option>
                </select>
            </div>
        </div>
    </div>
</body>
</html>

2
  • 1
    Provide jsfiddle or at least code snippet. Commented Dec 24, 2016 at 17:16
  • yes there are a few ways to do this with JS. what's your problem/question? What are your attempts to solving this? Where did you get stuck? Commented Dec 24, 2016 at 17:21

4 Answers 4

4

If i understood your requirement correctly. This is what you are looking for using jquery

$("#QuestionOptions").change(function(){
  $("#A,#B").hide();
  if($(this).val() == "A"){
    $("#A").show();
  }else{
     $("#B").show(); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
 <head></head>
 <body>
    <div id="myQuestions">
        <select id="QuestionOptions">
            <option value disalbe selected>What Day is it?</option>
            <option value="A">Monday</option>
            <option value="A">Tuesday</option>
            <option value="B">Wednesday</option>
            <option value="B">Thursday</option>
            <option value="B">Friday</option>
            <option value="A">Saturday</option>
            <optoin value="A">Sunday</optoin>
        </select>
    </div>
    <div id="myAnswers">
        <div id="A" style="display: none;">
            <div id="QuestionC">
                <p>Exapmle of question.</p>
            </div>
            <div id="QuestionD">
                <select id="QuestionOptionsD">
                    <option value disalbe selected>Choose Your Answer</option>
                    <option value="G">Answer G</option>
                    <option value="H">Answer H</option>
                </select>
            </div>
        </div>
        <div id="B" style="display: none;">
            <div id="QuestionE">
                <p>Exmaple of another question.</p>
            </div>
            <div id="QuestionF">
                <select id="QuestionOptionsF">
                    <option value disalbe selected>Choose Your Answer</option>
                    <option value="I">Answer I</option>
                    <option value="J">Answer J</option>
                </select>
            </div>
        </div>
    </div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

Check out my JSFidle. Most of code on javascript, remove some html element that doesn't needed.

Comments

0

With Dynamic options

var dynamic_options = {
  'A': [{
    value: "1",
    text: "Option 1"
  }, {
    value: "2",
    text: "Option 2"
  }],

  'B': [{
    value: "3",
    text: "Option 3"
  }, {
    value: "4",
    text: "Option 4"
  }]
};

$(function() {
  var answers = $('#myAnswers').find('select');
  $('#myQuestions').on('change', 'select', function(e) {
    var options = dynamic_options[this.value] || [];
    answers.find('option:gt(0)').remove();
    $.each(options, function(i, option) {
      answers.append($('<option>').prop(option));
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myQuestions">
  <select>
    <option value=''>What Day is it?</option>
    <option value="A">Monday</option>
    <option value="A">Tuesday</option>
    <option value="B">Wednesday</option>
    <option value="B">Thursday</option>
    <option value="B">Friday</option>
    <option value="A">Saturday</option>
    <optoin value="A">Sunday</optoin>
  </select>
</div>
<div id="myAnswers">
  <div>
    <p>Exapmle of question.</p>
  </div>
  <div>
    <select>
      <option value=''>Choose Your Answer</option>
    </select>
  </div>
</div>

BTW, to disable an option in select, use attribute disabled, whereas you have used disalbe.

Comments

0

The following code makes the option selected when the value is matched with the option value.

<html>
 <head></head>
 <body>
<label>Quantity:</label><br><br>
<select name="qty" id="qty">
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
    <option value="200">200</option>
    <option value="250">250</option>
    <option value="300">300</option>
    <option value="350">350</option>
    <option value="400">400</option>
    <option value="450">450</option>
    <option value="500">500</option>
</select>

<script>
     $(document).ready(function(){
       $('#qty option[value={{ $qty }}]').attr('selected','selected');
   });
</script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.