jQuery <select> get selected options from multiple select box

Description

jQuery <select> get selected options from multiple select box

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Get Values from Multiple Select Box</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function() {
    $("button").click(function(){
        var countries = [];//w  ww .j av  a2  s  .c o  m
        $.each($(".country option:selected"), function(){
            countries.push($(this).val());
        });
        document.getElementById("demo").innerHTML = 
           "You have selected the country - " + countries.join(", ");
    });
});
</script>
</head>
<body>
    
    <p id="demo"></p>
    
    
    
  <p>Press control key (Ctrl) to select multiple values:</p>
    <form>
        <label>Country:</label>
        <select class="country" multiple="multiple" size="5">
            <option>United States</option>
            <option>India</option>
            <option>United Kingdom</option>
            <option>Brazil</option>
            <option>Germany</option>
        </select>
        <button type="button">Get Values</button>
    </form>
</body>
</html>



PreviousNext

Related