ADVERTISEMENT
ADVERTISEMENT

JavaScript - Print Selected Item from SelectBox

JavaScript function to print selected item from SelectBox on onchange event, in this code we will select a city from given selectbox and print selected item.

Print Selected Item from SelectBox

JavaScript function:

<script type="text/javascript">
    function printSelectedItem() {
        var e = document.getElementById("cities");
        var selectedItem = e.options[e.selectedIndex].value;
        alert(selectedItem);
    }
</script>

HTML Source Code with JavaScript:

<!--JavaScript - Print Selected Item from SelectBox.-->
<html>
    <head>
        <title>JavaScript - Print Selected Item from SelectBox.</title>
        <script type="text/javascript">
            function printSelectedItem() {
                var e = document.getElementById("cities");
                var selectedItem = e.options[e.selectedIndex].value;
                alert(selectedItem);
            }
        </script>
    </head>
    <body style="text-align: center;">
        <h1>JavaScript - Print Selected Item from SelectBox.</h1>
        <big>Select your favourite city:</big>
        <select id="cities" onchange="printSelectedItem()">
            <option value="New york">New york</option>
            <option value="New jersey">New jersey</option>
            <option value="Washington DC">Washington DC</option>
        </select>
    </body>
</html>

Result:

print selected item from selectbox in JavaScript

JavaScript Examples »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.