0

I want to bind JSON data to <select> element. I created HTML page. Let me know where i was wrong. Look what i have tried..

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta charset="utf-8" />
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

        <script>
            function clickMe() {
                // THE JSON ARRAY.
                var cars = [
                    {
                        "ID": "001",
                        "Car_Name": "BMW"
                    },
                    {
                        "ID": "002",
                        "Car_Name": "AUDI"
                    },
                    {
                        "ID": "003",
                        "Car_Name": "Range Rover"
                    }
                ];

                var ele = document.getElementById('sel');
                for (var i = 0; i < cars.length; i++) {
                    // POPULATE SELECT ELEMENT WITH JSON.
                    ele.innerHTML = ele.innerHTML +
                        '<option value="' + cars[i]['ID'] + '">' + cars[i]['Car_Name'] + '</option>';
                }
            }

            function show(ele) {
                // GET THE SELECTED VALUE FROM <select> ELEMENT AND SHOW IT.
                var msg = document.getElementById('msg');
                msg.innerHTML = 'Selected Car: <b>' + ele.options[ele.selectedIndex].text + '</b> </br>' +
                    'ID: <b>' + ele.value + '</b>';
            }
        </script>
    </head>

    <body>
        <div>
            <div>
                <!--Click button-->
                <p style="margin-top:20px;">
                    <input type="button" onclick="clickMe"value="Click to Select" />
                </p>        

                <!--Select Menu-->
                <p>
                    <select id="sel" onchange="show(this)">
                        <option value=""> - - Select - -</option>
                    </select>
                </p>

                <p id="msg">
                </p>
            </div>
        </div>
    </body>
    </html>
3
  • 1
    you have to call function with closed brackets. onclick="clickMe()" Commented Sep 3, 2019 at 7:38
  • 1
    Instead of assigning to innerHTML in the loop, build up a string adding the options to it then assign this string to the innerHTML outside of the loop. Commented Sep 3, 2019 at 7:41
  • Voted to close due to small typographical error. Commented Sep 3, 2019 at 9:03

3 Answers 3

2

In my opinion, you should try out the ajax.

$.ajax({
    url:'test.html',
    type:'POST',
    data: 'test =' + str,
    dataType: 'json',
    success: function( json ) {
        $.each(json, function(i, value) {
            $('#myselect').append($('<option>').text(value).attr('value', value));
        });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Your HTML

<input type="button" onclick="clickMe" value="Click to Select" />

change to

<input type="button" onclick="clickMe()" value="Click to Select" />

is correct.

Comments

-1

Here, without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. So in order to call the function you need to change the statement from

<input type="button" onclick="clickMe" value="Click to Select" /> to

<input type="button" onclick="clickMe()" value="Click to Select" />

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.