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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:47:45+00:00 2026-06-06T02:47:45+00:00

I have this long list of checkboxes with specific labels that look something like

  • 0

I have this long list of checkboxes with specific labels that look something like this:

<input type='checkbox' name='check[]' id='159' value='159' />
<label for='159'>Person 1</label>

<input type='checkbox' name='check[]' id='160' value='160' />
<label for='160'>Person 2</label>

And below this form I have six div’s like so:

<div id="member1"></div>
<div id="member2"></div>
<div id="member3"></div>
<div id="member4"></div>
<div id="member5"></div>
<div id="member6"></div>

I have this JS function so that when I click on a checkbox, it’s label is inserted into the first div. Here’s what it looks like:

$(function(){
    $('input:checkbox').click(function(){
        var id = $(this).attr('id');
        if($(this).is(':checked')){
            $('#member1').text($('label[for="'+ id +'"]').text());
        }
        else{
            $('#member1').text('');   
        }
    });
});

So one problem is the function currently has to specify which div to put it into (#member1). It’s supposed to work that when the first div is “full”, the next checkbox will insert its label into the second div, and when that one is full, the third checkbox will insert its label into the third div, etc.

The other issue is if a checkbox becomes unchecked, its label should be removed from its div and the labels in the divs below it should move up. Anyone know if that’s possible? I’ll accept help for either problem!

  • 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-06T02:47:47+00:00Added an answer on June 6, 2026 at 2:47 am

    Caveat: I would do this completely differently.

    But using your current structure: Live copy | source (using valid ids)

    $(function(){
        $('input:checkbox').click(function(){
            var $cb = $(this),
                id  = this.id, // No need for `attr`
                member,
                prev;
            if(this.checked){  // No need for `attr`
                $("div[id^=member]").each(function() {
                    if (!this.firstChild) {
                        // It's empty, use it
                        $(this).text($('label[for="'+ id +'"]').text()).attr("data-contents", id);
                        return false; // Done
                    }
                });
            }
            else {
                member = $('div[data-contents="' + id + '"]');
                if (member[0]) {
                    member.empty().removeAttr('data-contents');
                    prev = member[0];
                    member.nextAll().each(function() {
                       if (!this.firstChild) {
                           return false; // Done
                       }
                       prev.appendChild(this.firstChild);
                       prev = this;
                    });
                }
            }
        });
    });
    

    Minimum changes I would make:

    • Put the #memberX divs in a container so we don’t have to do the id^= search.
    • Don’t use id values on the #memberX divs at all.
    • If you don’t absolutely, positively need them, don’t have empty #member divs at all. This would simplify the code markedly.
    • If you do need them, when clearing, remove the div and just append a new, empty one to the end.

    Example: Live copy | source

    HTML:

    Just replace the #membersX divs with <div id="members"></div>".

    JavaScript:

    $(function(){
        $('input:checkbox').click(function(){
            var $cb = $(this),
                id  = this.id; // No need for `attr`
    
            if(this.checked){  // No need for `attr`
                $("<div>")
                    .text($('label[for="'+ id +'"]').text())
                    .attr("data-contents", id)
                    .appendTo("#members");
            }
            else {
                $('div[data-contents="' + id + '"]').remove();
            }
        });
    });
    

    You can simplify even more by moving the checkbox input the labels (which also means you can do away with the for attribute): Live copy | source

    HTML:

    <label><input type='checkbox' name='check[]' id="x159" value='159' />
      Person 1</label>
    <label><input type='checkbox' name='check[]' id="x160" value='160' />
      Person 2</label>
    <div id="members"></div>
    

    JavaScript:

    $(function(){
        $('input:checkbox').click(function(){
            var id  = this.id; // No need for `attr`
    
            if(this.checked){  // No need for `attr`
                $("<div>")
                    .text($(this.parentNode).text())
                    .attr("data-contents", id)
                    .appendTo("#members");
            }
            else {
                $('div[data-contents="' + id + '"]').remove();
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this long list of checkboxes with specific labels that look something like
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have fancy looking jQuery Multiple checkbox/selectbox list script. Look HERE for demo. This
Hopefully this is simple: I have a relatively long list where each list item
This is a long shot, I know... Let's say I have a collection List<MyClass>
In Android 3.0 (Honeycomb) we have this feature that when we long press any
I have a long list of checkboxes bound in a repeater is there a
I have a long list of checkboxes each with a link next to it.
I have this long query that I finally got to work but I am
For classes that have a long list of setters that are used frequently, I

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.