Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
    var selectBrand = $("#brand");
    var selectType = $("#type");

    var optionsList = {
        nokia: [
        "C6-01",
        "E7-00"
    ],
        apple: [
        "iPhone 3",
        "iPhone 3G",
        "iPhone 4"
    ]
    };

    selectBrand.change(function () {
        var brand = selectBrand.val();
        var options = optionsList[brand];
        var html;

        if (options) {
            html = '<option value="">- select -</option>';
            $.each(options, function (index, value) {
                html += '<option value="' + value + '">' + value + '</option>';
            });
        } else {
            html = '<option value="">Select a brand</option>';
        }
        selectType.html(html);
    }).change();
</script>

</head>
<body>
    
    
<select id="brand">
    <option value="">- select -</option>
    <option value="nokia">Nokia</option>
    <option value="apple">Apple</option>
</select>

<hr>

<select id="type">
</select>
</body>
</html>
Posted
Comments
Andy Lanng 13-May-15 7:17am    
What is the error?
Where is the error?
What happens that you don't expect?
kiran gowda8 14-May-15 1:54am    
<select id="type">
</select>
it has to display the optionlist elements upon selecting nokia or apple. but its not displaying

1 solution

Try this, just added your scripts inside document ready event of jquery.

JavaScript
<script type="text/javascript">


        var optionsList = {
            nokia: [
            "C6-01",
            "E7-00"
            ],
            apple: [
            "iPhone 3",
            "iPhone 3G",
            "iPhone 4"
            ]
        };

        $(function () { // added your code inside document ready function
            var selectBrand = $("#brand");
            var selectType = $("#type");

          //  console.log('on load');
            selectBrand.change(function () {
              //  console.log('test');
                var brand = selectBrand.val();
                var options = optionsList[brand];
                var html;

                if (options) {
                    html = '<option value="">- select -</option>';
                    $.each(options, function (index, value) {
                        html += '<option value="' + value + '">' + value + '</option>';
                    });
                } else {
                    html = '<option value="">Select a brand</option>';
                }
                selectType.html(html);
            }).change();
        });
    </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900