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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:26:47+00:00 2026-05-17T15:26:47+00:00

How would I go about detecting the order in which checkboxes are checked? I

  • 0

How would I go about detecting the order in which checkboxes are checked? I have a list of checkboxes on a form, and I need to have users select their first and second choices (but no more). So, given this:

<input name="checkbox1" type="checkbox" value="a1"> Option 1
<input name="checkbox1" type="checkbox" value="a2"> Option 2
<input name="checkbox1" type="checkbox" value="a3"> Option 3
<input name="checkbox1" type="checkbox" value="a4"> Option 4

If someone selects option 2, then option 3, I’d like to have some indicator that option 2 was the first choice, and option 3 was the second choice. Any ideas?

Thanks in advance.

Update:

These are extremely helpful suggestions, thank you. As I test these examples, it’s giving me a better idea of how to approach the problem – but I’m still a bit stuck (I’m a JS novice). What I want to do is have these labels change as the checkboxes are checked or unchecked, to indicate which is the first or second selection:

<label id="lblA1"></label><input name="checkbox1" type="checkbox" value="a1"> Option 1
<label id="lblA2"></label><input name="checkbox1" type="checkbox" value="a2"> Option 2
<label id="lblA3"></label><input name="checkbox1" type="checkbox" value="a3"> Option 3
<label id="lblA4"></label><input name="checkbox1" type="checkbox" value="a4"> Option 4

So if someone clicks Option 2, then Option 3, lblA2 will display “First”, and lblA3 will display “Second”. If someone unchecks Option 2 while Option 3 is still checked, lblA3 becomes “First”. Hopefully this makes sense?

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-17T15:26:48+00:00Added an answer on May 17, 2026 at 3:26 pm

    If you are using jQuery. Below code is does what you have explained and it is tested.
    I have used global variables.

    <input name="checkbox1" type="checkbox" value="a1" /> Option 1
    <input name="checkbox1" type="checkbox" value="a2" /> Option 2
    <input name="checkbox1" type="checkbox" value="a3" /> Option 3
    <input name="checkbox1" type="checkbox" value="a4" /> Option 4
    <input type="button" value="do" id="btn" />
    

    As shown below, it also handles the situation that user unchecks a choice.

    $(document).ready(function () {
        var first = "";
        var second = "";
        $('input[name="checkbox1"]').change(function () {
            if ($(this).attr('checked')) {
                if (first == "") {
                    first = $(this).attr('value');
                }
                else if (second == "") {
                    second = $(this).attr('value');
                }
            }
            else {
                if (second == $(this).attr('value')) {
                    second = "";
                }
                else if (first == $(this).attr('value')) {
                    first = second;
                    second = "";
                }
            }
        });
        $('#btn').click(function () {
            alert(first);
            alert(second);
        });
    });
    

    I hope that it will be helpful.

    UPDATE [IMPORTANT]:

    I have noticed that my previous code was incomplete, for example, if you check a1, then a2, then a3, then uncheck a2; my code was not recognising a3 as second.
    Here is the complete solution of your updated problem. I used array this time.

    The complete HTML:

    <label id="lblA1"></label>
    <input name="checkbox1" type="checkbox" value="a1" /> Option 1
    <label id="lblA2"></label>
    <input name="checkbox1" type="checkbox" value="a2" /> Option 2
    <label id="lblA3"></label>
    <input name="checkbox1" type="checkbox" value="a3" /> Option 3
    <label id="lblA4"></label>
    <input name="checkbox1" type="checkbox" value="a4" /> Option 4
    

    The complete Javascript:

    $(document).ready(function () {
        var array = [];
        $('input[name="checkbox1"]').click(function () {
            if ($(this).attr('checked')) {
                // Add the new element if checked:
                array.push($(this).attr('value'));
            }
            else {
                // Remove the element if unchecked:
                for (var i = 0; i < array.length; i++) {
                    if (array[i] == $(this).attr('value')) {
                        array.splice(i, 1);
                    }
                }
            }
            // Clear all labels:
            $("label").each(function (i, elem) {
                $(elem).html("");
            });
            // Check the array and update labels.
            for (var i = 0; i < array.length; i++) {
                if (i == 0) {
                    $("#lbl" + array[i].toUpperCase()).html("first");
                }
                if (i == 1) {
                    $("#lbl" + array[i].toUpperCase()).html("second");
                }
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about detecting whitespace between strings? For example, I have a
How would one go about detecting if two images on the screen (form) were
If I have the strings hello8459 and 1234, how would I go about detecting
How would you go about dead code detection in C/C++ code? I have a
Anyone have any idea how I would go about converting a timestamp in milliseconds
How would one go about detecting a page refresh / F5 key push on
I was wondering if anybody knows how I would go about detecting when the
I was wondering how I would go about detecting if when a person likes
I am making a site which allows users to sign up and record their
I'm interested in knowing how I would go about detecting a primitive circle touching

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.