How can we avoid use of button in the following code : Can we select just " on change " of multiselect dropdown

How can we avoid use of button in the following code : Can we select just " on change " of multiselect dropdown and ajax post these value indexes

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="utf-8">

    <title>jQuery Get Multiple Selected Option Value</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function() {

        $("button").click(function(){

            var countries = [];

            $.each($(".country option:selected"), function(){            

                countries.push($(this).val());

            });

            alert("You have selected the country - " + countries.join(", "));

        });

    });

    </script>

    </head>

    <body>

        <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>
Puneet AroraFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Just replace this line:
$("button").click(function(){

Open in new window


with this one:
$('select').on('change', function(){

Open in new window


See here: https://api.jquery.com/change/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
And, please, when you post code here at EE use the code tag: in the toolbar above the text box where you type quetions and comments there's the button 'Code'; click it and put the code within the opening and closing tags, so it wil be easier for us to read and to understand it. Thank you.
Ray PaseurCommented:
Marco has given you a good answer -- it is technically correct and I have only one thing to add.

You can do this, but I advise against it.  People are not used to having a changed selection trigger an action - what if they make a typo or want to change the selection more than once?  I think it is better to let all of the controls on your web pages work in an intuitive manner -- the way they are expected to work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.