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.

JavaScript Adding Different Dropdown Menus

Hello

I am currently working on reviving an old fansite of mine. I have finished designing the main site elements across the whole fansite in photoshop, and have begun coding them in HTML. One of the elements I have created is a series of 3 dropdown menus for a quick navigation of the games in the series. Each one represents a different category of games. I have never written javascript before so I am an absolute beginner. The javascript base code I have decided to use comes from this website: https://www.w3schools.com/howto/howto_js_dropdown.asp I have gotten one menu to show, but I am unsure of how to create the other menus. I have tried copying the code, and changing the name of the myfunction and the name of the event function, but no menus show up unless I remove the additional javascript. Do I need to alter more of the code. I thank anyone who reads this.
 
Solution
This is done in JS from https://benalexkeen.com/auto-updating-dropdown-fields-in-javascript/
Added so I could see this done in css.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form>
    <select id="ddl1" onchange="configureDDL2(this, document.getElementById('ddl2'), document.getElementById('ddl3'))">
        <option value="">Pick First Option</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select id="ddl2" onchange="configureDDL3(document.getElementById('ddl1'), this...
This is what I am trying to create:

header-nav_dropdown-example.jpg

This is the javascript that I am using:


JavaScript:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
 
This is done in JS from https://benalexkeen.com/auto-updating-dropdown-fields-in-javascript/
Added so I could see this done in css.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form>
    <select id="ddl1" onchange="configureDDL2(this, document.getElementById('ddl2'), document.getElementById('ddl3'))">
        <option value="">Pick First Option</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select id="ddl2" onchange="configureDDL3(document.getElementById('ddl1'), this, document.getElementById('ddl3'))">
        <option>Make selection in one first</option>
    </select>
    <select id="ddl3">
        <option>Make selection in one first</option>
    </select>
    </form>
    
    <script>
var myNestedVals = {
        '1': {
            '1.1': ['1.1.1', '1.1.2', '1.1.3'],
            '1.2': ['1.2.1', '1.2.2', '1.2.3'],
            '1.3': ['1.3.1', '1.3.2', '1.3.3']
        },
        '2': {
            '2.1': ['2.1.1', '2.1.2', '2.1.3'],
            '2.2': ['2.2.1', '2.2.2', '2.2.3'],
            '2.3': ['2.3.1', '2.3.2', '2.3.3']
        },
        '3': {
            '3.1': ['3.1.1', '3.1.2', '3.1.3'],
            '3.2': ['3.2.1', '3.2.2', '3.2.3'],
            '3.3': ['3.3.1', '3.3.2', '3.3.3']
        }
    }

    function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }

    function createOptions(optionsArray, ddl) {
        for (i = 0; i < optionsArray.length; i++) {
            createOption(ddl, optionsArray[i], optionsArray[i]);
        }
    }

    function configureDDL2(ddl1, ddl2, ddl3) {
        ddl2.options.length = 0;
        ddl3.options.length = 0;
        createOption(ddl2, "Pick 2nd Option", "");
        var ddl2keys = Object.keys(myNestedVals[ddl1.value]);
        createOptions(ddl2keys, ddl2);
        createOption(ddl3, "Make selection in second list first", "");
    }

    function configureDDL3(ddl1, ddl2, ddl3) {
        ddl3.options.length = 0;
        createOption(ddl3, "Pick 3rd Option", "");
        var ddl3keys = myNestedVals[ddl1.value][ddl2.value];
        createOptions(ddl3keys, ddl3);
    }
</script>
</body>
</html>

As you can see you need to make some changes. try it and if you like it come back and ask for those changes.
P.S. I don't like it, but it's a place to start.
 
Solution
This is done in JS from https://benalexkeen.com/auto-updating-dropdown-fields-in-javascript/
Added so I could see this done in css.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form>
    <select id="ddl1" onchange="configureDDL2(this, document.getElementById('ddl2'), document.getElementById('ddl3'))">
        <option value="">Pick First Option</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select id="ddl2" onchange="configureDDL3(document.getElementById('ddl1'), this, document.getElementById('ddl3'))">
        <option>Make selection in one first</option>
    </select>
    <select id="ddl3">
        <option>Make selection in one first</option>
    </select>
    </form>
   
    <script>
var myNestedVals = {
        '1': {
            '1.1': ['1.1.1', '1.1.2', '1.1.3'],
            '1.2': ['1.2.1', '1.2.2', '1.2.3'],
            '1.3': ['1.3.1', '1.3.2', '1.3.3']
        },
        '2': {
            '2.1': ['2.1.1', '2.1.2', '2.1.3'],
            '2.2': ['2.2.1', '2.2.2', '2.2.3'],
            '2.3': ['2.3.1', '2.3.2', '2.3.3']
        },
        '3': {
            '3.1': ['3.1.1', '3.1.2', '3.1.3'],
            '3.2': ['3.2.1', '3.2.2', '3.2.3'],
            '3.3': ['3.3.1', '3.3.2', '3.3.3']
        }
    }

    function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }

    function createOptions(optionsArray, ddl) {
        for (i = 0; i < optionsArray.length; i++) {
            createOption(ddl, optionsArray[i], optionsArray[i]);
        }
    }

    function configureDDL2(ddl1, ddl2, ddl3) {
        ddl2.options.length = 0;
        ddl3.options.length = 0;
        createOption(ddl2, "Pick 2nd Option", "");
        var ddl2keys = Object.keys(myNestedVals[ddl1.value]);
        createOptions(ddl2keys, ddl2);
        createOption(ddl3, "Make selection in second list first", "");
    }

    function configureDDL3(ddl1, ddl2, ddl3) {
        ddl3.options.length = 0;
        createOption(ddl3, "Pick 3rd Option", "");
        var ddl3keys = myNestedVals[ddl1.value][ddl2.value];
        createOptions(ddl3keys, ddl3);
    }
</script>
</body>
</html>

As you can see you need to make some changes. try it and if you like it come back and ask for those changes.
P.S. I don't like it, but it's a place to start.
Are you talking about the way I designed it, or just the code you mentioned above? If it is my design, do you have any suggestions on how to improve it?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom