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 938851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:42:07+00:00 2026-05-15T21:42:07+00:00

i have a multiple select tag, look at script please <select multiple=multiple size=5 id=cities_select>

  • 0

i have a multiple select tag, look at script please

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

and jquery script:

$("#supply_cities_select").change(function()
{
    var i = 1;
    $('#supply_cities_select :selected').each(function(u) 
    {
         src += '&c'+i+'='+$(this).val();//generates the string like &c1=1&c2=2&c3=7...
         i++;
    });
})

i need my string not to have elements greater then 5

example:

if i allready have 5 selected elements, my string looks like

&c1=2&c2=3&c3=5&c4=6&c5=7

now if one more option will be selected, i need to get the string

 &c1=3&c2=5&c3=6&c4=7&c5=8

if be short, i need to remove selected attribute of first selected element.

(but i can’t use .first here, because it can be element N8 the first selected)

how can i do it?

Thanks a lot.

UPDATE

var a = $("#supply_cities_select :selected").length;
if(a > 5)
{
     $("#supply_cities_select :selected:lt(1)").attr("selected",false);
}

it just remove the firs selected option, isn’t it?


  • 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-15T21:42:08+00:00Added an answer on May 15, 2026 at 9:42 pm

    You can do this a little simpler usign .map() and :lt() like this:

    var src;
    $("#supply_cities_select").change(function() {
      src = $("#supply_cities_select :selected:lt(5)").map(function(i) {
          return '&c'+(i+1)+'='+this.value; //i starts at 0, so add 1
      }).get().join('');
    })​;
    

    You can try a demo here. The :lt() selector gets the first 5 (less than 5, 0-based, so (0-4), then we’re using .map() to get the values into an array, then just calling .join() to get a string of that array added together.


    For the update: to get the last 5 elements it’s better to use .slice(), like this:

    var src;
    $("#supply_cities_select").change(function() {
      src = $("#supply_cities_select :selected").slice(-5).map(function(i) {
          return '&c'+(i+1)+'='+this.value; //i starts at 0, so add 1
      }).get().join('');
    })​;
    

    You can give it a try here.

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

Sidebar

Related Questions

i have a multiple select tag <select multiple=multiple size=5 id=cities_select> <option value=1>city1</option> <option value=2>city2</option>
So I have two multiple select boxes like this <select id=foo multiple=multiple> <option value=1>Option
I have this HTML: <select multiple='multiple' size='3'> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> Using
< select size=2 multiple> < option value=1>1< /option> < option value=2>2< /option> < option
I have a multiple select tag, and I need to write the function onclick
can we get the multiple values from a select tag through Ajax script in
I have this piece of HTML <select multiple= name=colonia_id[] id=colonia_id[] onchange= size=10></select> I need
I have a code that removes an option element from a select multiple( <select
I have a select field, with the multiple tag. Right now, the field has
I have written a script which select and deselect all options of multiple select

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.