Arun Sunderraj
Arun Sunderraj

Reputation: 37

Error with Concatenate values from DropDown using JS

i have written the HTML and JS for concatenating the value from the dropdowns. But I am not able to get the result from it.

Code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Selected Option Value</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $(".country",'.state').on('change',function(){
        var selectedCountry = $(".country").find("option:selected").val();
        var selectedState = $(".state").find("option:selected").val();
        document.getElementById('demo').innerHTML= selectedCountry + " " + selectedState;
    });
});
</script>
</head> 
<body>
    <form>
        <label>Select Country:</label>
        <select class="country">
            <option value="usa">United States</option>
            <option value="india">India</option>
            <option value="uk">United Kingdom</option>
        </select>
        <select class="state">
            <option value="Washington">Washington</option>
            <option value='New Delhi'>New Delhi</option>
            <option value='London'>London</option>
        </select>
    </form>
    <p id="demo"></p>
</body>
</html>                            

Upvotes: 0

Views: 31

Answers (1)

user13046635
user13046635

Reputation:

Selector for Eventhandler is incorrect, and you can get the value of select box by $(selectboxSelector).val() easily.

$(document).ready(function(){
    $('.country, .state').on('change',function(){
        var selectedCountry = $(".country").val();
        var selectedState = $(".state").val();
        $('#demo').html( selectedCountry + " " + selectedState );
    });
});

Upvotes: 1

Related Questions