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

  • Home
  • SEARCH
  • 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 8861263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:27:16+00:00 2026-06-14T15:27:16+00:00

I have a simple select form in HTML with some items. I’m using javascript

  • 0

I have a simple select form in HTML with some items. I’m using javascript to limit the number of items the user can select.

In the HTML I have the following code:

<select name="category" multiple id="category" onchange="checkMaxSelected(this, 3, 'Max number of categories you can select: ');">
    <option value="orange">orange</option>
    <option value="apple">apple</option>
    <option value="banana">banana</option>
    <option value="pear">pear</option>
    <option value="strawberry">strawberry</option>
    </select>

I pass three variables to the javascript function checkMaxSelected:

  1. which select form (i.e. this)
  2. the max number of selections the user can perform in the form (in my case, 3)
  3. the error message that should be displayed in an alert box.

The JS code is the following:

function checkMaxSelected (select, maxSelected, displ_error_nummaxcat) {
  if (!select.storeSelections) {
   select.storeSelections = new Array(select.options.length);
   select.selectedOptions = 0;
   }

   for (var i = 0; i < select.options.length; i++) {

  console.log('select.options[i].selected: '+select.options[i].selected+' select.storeSelections[i]: '+select.storeSelections[i]);

     if (select.options[i].selected && 
         !select.storeSelections[i]) {
       if (select.selectedOptions < maxSelected) {
         select.storeSelections[i] = true;
         select.selectedOptions++;
       }
      else {
        alert(displ_error_nummaxcat + maxSelected);
        console.log('HERE I SHOW ALERT!');
    select.options[i].selected = false; 
      }
    }
     else if (!select.options[i].selected &&
       select.storeSelections[i]) {
       select.storeSelections[i] = false;
       select.selectedOptions--;
     }
  }
};

This solution works perfectly under Firefox, Safari and IE, however it doesn’t work with Chrome. Why?

Any help is greatly appreciated. Thank you in advance.

Extra info:

I’ve been trying in vain to debug it using firebug both for firefox and Chrome: with Firefox when I select the first item of the select form I see this the firebug console:

select.options[i].selected: true select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined

(Which is correct).

The same action with Chrome (i.e. selecting the first item) triggers directly the error message. Here is the firebug console output in Chrome:

select.options[i].selected: true select.storeSelections[i]: undefined
HERE I SHOW ALERT!
select.options[i].selected: false
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
select.options[i].selected: false select.storeSelections[i]: undefined
  • 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-06-14T15:27:17+00:00Added an answer on June 14, 2026 at 3:27 pm

    To make your code work the same in Chrome as well as the other browsers, you need to rename your select.selectedOptions property to something else.

    The SELECT tag has a selectedOptions attribute that only Chrome is getting correctly, it appears, so while the other browsers take that property as your own created numeric property, for Chrome it is actually your selected option elements collection, that’s why your if (select.selectedOptions < maxSelected) statement is failing.

    Here’s your fixed Javascript function:

    function checkMaxSelected (select, maxSelected, displ_error_nummaxcat) {
        if (!select.storeSelections) {
            select.storeSelections = new Array(select.options.length);
            select.optionsSelected = 0;
        }
    
        for (var i = 0; i < select.options.length; i++) {
            console.log('select.options[i].selected: '+select.options[i].selected+' select.storeSelections[i]: '+select.storeSelections[i]);
            if (select.options[i].selected && !select.storeSelections[i]) {
                if (select.optionsSelected < maxSelected) {
                    select.storeSelections[i] = true;
                    select.optionsSelected++;
                }
                else {
                    alert(displ_error_nummaxcat + maxSelected);
                    console.log('HERE I SHOW ALERT!');
                    select.options[i].selected = false;
                }
            }
            else if (!select.options[i].selected && select.storeSelections[i]) {
                select.storeSelections[i] = false;
                select.optionsSelected--;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple HTML Form with one column having a select menu with
In a Struts 1.x application, I have a form with a simple single-selection html:select
I'm having some issues with dijit.form.Select items. In a page i have two of
I have a simple html form: <form action="/api/userattributes" method=get> <select name=action> <option>read_by_user</option> <option>set</option> <option>delete</option>
I have a simple html form and some php that input the POST variables
I am trying to create simple user registration form. I have an index.html file
I have a simple dropdown menu: <form class=feedmenu align=center> <select onchange=showRSS1(this.value)> <option value=>Select an
I have written a simple CRUD form which has one select list. However the
I have the following simple select statement : select 'hello' || '|' || 'world'
I have a simple select form. When a year is selected from the form

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.