How to Create Dropdown List Example Using JavaScript?

Nov 13, 2021 . Admin



Hello Friends,

Now let's see example of how to create dropdown list example. We will check how to create dropdown list. This is a short guide on create dropdown list in javascript. Here you will learn how to create dropdown list. Let's get started with how to create dropdown list in javascript.

Here i will give you many example how to create dropdown list using javascript.

Example : 1
<html>
    <head>
        <title>How to Create Dropdown List Example Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <form>
            <h3>How to Create Dropdown List Example Using JavaScript? - MyWebtuts.com</h3>
            <b>Select Your Favourite Tutorial Site Using Dropdown List :</b>
            <select id = "a1" onchange = "a2()" >
                <option>Choose tutorial</option>
                <option>MyWebtuts</option>
                <option>NiceSnippet</option>
                <option>ItSolutionStuff</option>
            </select>
            <p>Your selected tutorial site is : 
                <input type = "text" id = "subj" size = "20">
            </p>
        </form>
        <script>
            function a2() {
                var a1 = document.getElementById("a1");
                document.getElementById("subj").value = a1.options[a1.selectedIndex].text;
            }
        </script>
    </body>
</html>
Output :
Select Your Favourite Tutorial Site Using Dropdown List : MyWebtuts
Your selected tutorial site is : MyWebtuts
Example : 2
<html>
    <head>
        <title>How to Create Dropdown List Example Using JavaScript? - MyWebtuts.com</title>
        <style>
            .dbtn {
                background-color: blue;
                color: white;
                padding: 14px;
                font-size: 16px;
                cursor: pointer;
            }

            .dbtn:hover {
                background-color: gray;
            }

            .dropdown {
                position: relative;
                display: inline-block;
            }

            .dropdown-content {
                display: none;
                position: absolute;
                background-color: white;
                min-width: 140px;
                overflow: auto;
                box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            }

            .dropdown-content a {
                color: black;
                padding: 12px 16px;
                text-decoration: none;
                display: block;
            }

            .dropdown a:hover {
                background-color: #ddd;
            }

            .show {
                display: block;
            }
        </style>
    </head>
    <body>
        <h3>How to Create Dropdown List Example Using JavaScript? - MyWebtuts.com</h3>
        <p>List of tutorials using Dropdown menu</p>
        <p>Click on the button to open the tutorial dropdown menu.</p>

        <div class="dropdown">
            <button onclick="prgmList()" class="dbtn">Programming</button>
            <div id="a1" class="dropdown-content">
                <a href="#laravel" onclick="laravel()">Laravel</a>
                <a href="#php" onclick="php()">Php</a>
                <a href="#JQuery" onclick="JQuery()">JQuery</a>
                <a href="#laravel8" onclick="laravel8()">Laravel 8</a>
            </div>
        </div>
        <script>
            function prgmList() {
                document.getElementById("a1").classList.toggle("show");
            }

            function laravel() {
                window.location.replace("https://www.mywebtuts.com/blog/cat/laravel");
            }
            function php() {
                window.location.replace("https://www.mywebtuts.com/blog/cat/php");
            }
            function JQuery() {
                window.location.replace("https://www.mywebtuts.com/blog/cat/jquery");
            }
            function laravel8() {
                window.location.replace("https://www.mywebtuts.com/blog/cat/laravel-8");
            }

            window.onclick = function(event) {
                if (!event.target.matches('.dbtn')) {
                    var dropdwn = document.getElementsByClassName("dropdown-content");
                    var i;
                    for (i = 0; i < dropdwn.length; i++) {
                        var openDropdown = dropdwn[i];
                        if (openDropdown.classList.contains('show')) {
                            openDropdown.classList.remove('show');
                        }
                    }
                }
            }
        </script>
    </body>
</html>
Output :
Click on the button to open the tutorial dropdown menu.
Programmig
	Laravel
	Php
	JQuery
	Laravel 8

It will help you...

#Javascript