loader image

JavaScript program to display capital of the selected country

Write a JavaScript program to display the capital of a country using onchange() event. The county is selected from a select box and capital is displayed on a TextBox.

<html>

<body>
    Select a country :
    <select id="countries" onchange=getCapital()>
        <option value="0">Australia</option>
        <option value="1">Poland</option>
        <option value="2">Mexico</option>
        <option value="3">Germany</option>
        <option value="4">India</option>
    </select>
    <br/><br/>
    Capital: <input type="text" id="txtbox">
    <script>
        function getCapital() {
            var capitals=["Canberra","Warsaw","Mexico City","Berlin","New Delhi"];
            var i = document.getElementById("countries").selectedOptions[0].value;
            document.getElementById("txtbox").value = capitals[i];
        }
    </script>

</body>

</html>
Output
select box js 1
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments