Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:11:43+00:00 2026-05-15T22:11:43+00:00

i have a multiple select tag <select multiple=multiple size=5 id=cities_select> <option value=1>city1</option> <option value=2>city2</option>

  • 0

i have a multiple select tag

<select multiple="multiple" size="5" id="cities_select">
     <option value="1">city1</option>
     <option value="2">city2</option>
     <option value="3">city3</option>
     <option value="4">city4</option>
     <option value="5">city5</option>
     <option value="6">city6</option>
     ................................
</select>

i need to have max 5 selected elements, ie

if i have selected 1,2,3,4,5 elements, onselect od 6th element i need to remove selected attribute of first, ie, i must get 2,3,4,5,6 selected elements.

and now, what is the problem, if i have selected 2,3,4,5,6 for example, onselect of first i must remove selected attribute of last selected element, and get
1,2,3,4,5 selected list.
how can i get that effect?(i can’t fix the element, which edited last).

$("#supply_cities_select").change(function()
        {
            var a = $("#supply_cities_select :selected").length;
            if(a > 5)
            {
                //i don't know what to write here:(
            }
        })

Any ideas?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:11:44+00:00Added an answer on May 15, 2026 at 10:11 pm

    To get such effect i can’t help use .change method, so i wrote the function onclick event

    $("#supply_cities_select option").click(function()
            {
                var clicked = 0;
                if($(this).attr("selected")) 
                {
                    clicked = $(this).val();
                }
                if(clicked != 0)
                {
                    var a = $("#supply_cities_select :selected").length;
                     end = $("#supply_cities_select :selected").slice(-1).map(function(i) {
                          return this.value;
                      }).get().join('');
                    if(a > 5)
                    {
                        var v1 = parseInt(clicked);
                        var v2 = parseInt(end);
                        if(v1 < v2)
                        {
                            $("#supply_cities_select option[value='"+v2+"']").attr("selected",false);
    
                        }
                        if(v1 == v2)
                        {
                            $("#supply_cities_select :selected:lt(1)").attr("selected",false);
                        }
    
                    }
                }
            });
    

    you can see the demo here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.