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">


    <meta charset="utf-8">

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

    <script type="text/javascript" src=""></script>

    <script type="text/javascript">

    $(document).ready(function() {


            var countries = [];

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



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








            <select class="country" multiple="multiple" size="5">

                <option>United States</option>


                <option>United Kingdom</option>




            <button type="button">Get Values</button>



Puneet AroraFounderAsked:
Who is Participating?

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

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:

Open in new window

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

Open in new window

See here:

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

From novice to tech pro — start learning today.