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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:11:52+00:00 2026-06-06T06:11:52+00:00

I have to fix a glitch with this code someone wrote but I’m not

  • 0

I have to fix a glitch with this code someone wrote but I’m not skilled enough at jQuery to figure it out myself. Maybe someone here can help me. First I’ll try explain what is supposed to happen…

Basically the idea is that you have a list of people with checkboxes next to their names. And below them you have six available slots (empty divs). When you check a box, the person’s name is inserted into one of the empty divs. Up to a maximum of six obviously.

The tricky part is if you uncheck a box you checked, the name should disappear from the slot, and all the names below it should move up one slot to fill the gap. This is where the glitch is happening. If you uncheck a box twice in a row, the second time nothing happens, or the third, etc…

This is the jQuery function we have so far:

$(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;
            });
        }
    }
});
});

Here’s an example of one of the checkboxes and some of the divs:

<input type='checkbox' name='check[]' id='155' value='155'/>
<label for='155'>John Doe</label>

<input type='checkbox' name='check[]' id='156' value='156'/>
<label for='156'>John Smith</label>

and

<div id="member1" class="member"></div>
<div id="member2" class="member"></div>
  • 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-06T06:11:53+00:00Added an answer on June 6, 2026 at 6:11 am

    You have some strangely un-jQuery-like behavior in your script, but we’ll let that go for now.

    The problem, as I saw it in my browser’s DOM inspector, is that when you unchecked a box it might reassign the contents of the second DIV to the first one, but it wouldn’t reassign the data-contents attribute at the same time.

    Modified code:

     prev.appendChild(this.firstChild);
     $(prev).attr('data-contents', $(this).attr('data-contents'));
     $(this).removeAttr('data-contents');
     prev = this;
    

    http://jsfiddle.net/q6Vg9/

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

Sidebar

Related Questions

OK I'm a beginner and I have to fix this java expression language code
I am not an ActionScript expert, but I unfortunately have to fix some broken
I have to fix some form validation but there's no jQuery included in the
I have a block of someone else's code that does essentially this: declare X
i have to fix the header in a gridview and i use this in
I have set a fix height of DIV and set its overflow-y:scroll but the
I have this weird bug that I cannot fix. Can anyone help me? Thank
I have seen oodles of examples to fix IE8's z-index insanity but none of
Does anybody know how to fix the glitch in jquery where the sliding block
This scripting language doesn't have a % or Mod(). I do have a Fix()

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.