2

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" class="member"></div>
<div id="member2" class="member"></div>
<div id="member3" class="member"></div>
<div id="member4" class="member"></div>
<div id="member5" class="member"></div>
<div id="member6" class="member"></div>

Someone helped me write 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(){
$('#159').click(function(){
    if($(this).is(':checked')){
        $('#member1').text($('label[for="159"]').text());
    }
    else{
        $('#member1').text('');   
    }
});
});

The problem is I don't know how to edit this function to work for any checkbox that's clicked. Currently it only works on the checkbox with an id of 159. The second problem is the function specifies what div to put it into. The idea is that when the first div is "full", the next checkbox will insert the label into the second div, and then the third, etc.

Please note that if the checkbox is unchecked, the label is removed from the div, and the labels in the divs below it would have to move up. Anyone know if this is possible?

3 Answers 3

2

try this:

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

   $(function(){
    $('input:checkbox').click(function(){
       var id = $(this).attr('id');
        if($(this).is(':checked')){
          if($('#memebr1').text() == "") {
            $('#member1').text($('label[for="'+ id +'"]').text());
          } else {
            $('#member2').text($('label[for="'+ id +'"]').text())
          }
        }
        else{
            $('#member1').text('');   
        }
    });
    });
Sign up to request clarification or add additional context in comments.

4 Comments

That certainly fixed the id problem. But do you know if there's a way to choose #member1 and if #member1 already has a label, choose #member2?
@AzzyDude yes, that was a typo, try it again.
Now it's saying "#member1".text is not a function. Adil has the right idea.
@AzzyDud that was another typo.
1

Try this.

Live Demo

   $(function() {

    $('input[type=checkbox][name=check\[\]]').click(function() {
        chkboxid = $(this).attr('id');
        if ($(this).is(':checked')) {

            $('#member' + eval($(this).index() + 1)).text($('label[for="' + chkboxid + '"]').text());

        }
        else {
            $('#member' + eval($(this).index() + 1)).text('');
        }
    });
});​

6 Comments

That works! Would it be much more difficult to augment the code so that if you uncheck the checkbox, the name is removed from the div?
Name from which div should be removed as there are many div and there is not correspondence between checkboxes and divs?
Hmm, well the idea is that you're making a list out of names. But if you change your mind and want to remove a name from the list (by unchecking the box) the name should just disappear and the names lower down in the list would move up. That make sense?
I have updated my answer, I hope this is what you want. Please have a look.
Hey, thanks for getting back to me! I pasted your function in with no errors but now nothing seems to be happening? (How it's working in the fiddle is perfect though!)
|
1
$(function(){
$('input[type=checkbox]').click(function(){
    if($(this).is(':checked')){
        $('#member1').text($('label[for="'+$(this).attr("id")+'"]').text());
    }
    else{
        $('#member1').text('');   
    }
});
});

4 Comments

That certainly fixed the id problem. But do you know if there's a way to choose #member1 and if #member1 already has a label, choose #member2?
$(".member").each(function(){ if($(this).html() == ""){ $(this).html($('label[for="'+$(this).attr("id")+'"]').text()); exit; } });
replace $('#member1').text($('label[for="'+$(this).attr("id")+'"]').text()); this line by above code
Hmm, no errors, but now nothing is happening. Adil above got it working though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.