Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How do I create a bunch of cascading select inputs that in the end show one specific set of divs?

I want to create select inputs that do the following:
1st One: Program Name
- Link each program name to another select input

2nd One: Belt Rank
- A different set of belt ranks will display according to the previous option selected in the first select input.

Overall, the function will result in displaying only the divs that match the inputs given by the user. If nothing is selected on page load, display a message that says: No program selected. When a program is selected, hide that message. I have no code because I don't know how to do any of this. Please help!
 
I think I talked about learning code from W3Schools. This was taken from that site under the title: How TO - Cascading Dropdown List

JS is one way if the choices are not many, then communicating with a PHP/DB would be used.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Table Example</title>
</head>
<body>
    <form name="form1" id="form1" action="/action_page.php">
        Subjects: <select name="subject" id="subject">
          <option value="" selected="selected">Select subject</option>
        </select>
        <br><br>
        Topics: <select name="topic" id="topic">
          <option value="" selected="selected">Please select subject first</option>
        </select>
        <br><br>
        Chapters: <select name="chapter" id="chapter">
          <option value="" selected="selected">Please select topic first</option>
        </select>
        <br><br>
        <input type="submit" value="Submit">
      </form>

<script>
var subjectObject = {
  "Front-end": {
    "HTML": ["Links", "Images", "Tables", "Lists"],
    "CSS": ["Borders", "Margins", "Backgrounds", "Float"],
    "JavaScript": ["Variables", "Operators", "Functions", "Conditions"]
  },
  "Back-end": {
    "PHP": ["Variables", "Strings", "Arrays"],
    "SQL": ["SELECT", "UPDATE", "DELETE"]
  }
}
window.onload = function() {
  var subjectSel = document.getElementById("subject");
  var topicSel = document.getElementById("topic");
  var chapterSel = document.getElementById("chapter");
  for (var x in subjectObject) {
    subjectSel.options[subjectSel.options.length] = new Option(x, x);
  }
  subjectSel.onchange = function() {
    //empty Chapters- and Topics- dropdowns
    chapterSel.length = 1;
    topicSel.length = 1;
    //display correct values
    for (var y in subjectObject[this.value]) {
      topicSel.options[topicSel.options.length] = new Option(y, y);
    }
  }
  topicSel.onchange = function() {
    //empty Chapters dropdown
    chapterSel.length = 1;
    //display correct values
    var z = subjectObject[subjectSel.value][this.value];
    for (var i = 0; i < z.length; i++) {
      chapterSel.options[chapterSel.options.length] = new Option(z[i], z[i]);
    }
  }
}
</script>
</body>
</html>

Did not know what 'belt ranks" were so did not comment on them.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom