tags:

views:

32

answers:

3

Hey guys, so I would like you to help with an issue that I'm having, pretty much what i need is when the checkbox is clicked to append the checkbox value to an li element in a different div. Check the html below. Thanks in advance.

<div class="heading">
    <p><a href="#">Experiences</a></p>
    <a href="#" target="_self">modify</a>                
</div><!--heading-->
<div class="add-filters">
    <div class="inner">
        <h4 class="title-filtery">Filtery By:</h4>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td><input type="checkbox" id="adventure" name="adventure" /> <label for="adventure">Adventure</label></td>
                <td><input type="checkbox" id="diving" name="diving" /> <label for="diving">Diving</label></td>
            </tr>
            <tr>
                <td><input type="checkbox" id="beach" name="beach" /> <label for="beach">Beach</label></td>
                <td><input type="checkbox" id="ecotour" name="ecotour" /> <label for="ecotour">Eco-Tour</label></td>
            </tr>
            <tr>
                <td><input type="checkbox" id="boating" name="boating" /> <label for="boating">Boating</label></td>
                <td><input type="checkbox" id="family" name="family" /> <label for="family">Family</label></td>
            </tr>
            <tr>
                <td><input type="checkbox" id="canoe" name="canoe" /> <label for="canoe">Canoe/Kayak/Raft</label></td>
                <td></td>
            </tr>
        </table>
        <div class="btn-holder clearfix">
            <input type="button" class="btn-cancel" value="" />
            <input type="button" class="btn-update" value="" />
        </div>
    </div>
</div><!-- filters -->
<div class="hidden-filters">
    <p>Filtering by:</p>
    <ul>

    </ul>
</div><!-- hidden-filters -->
+1  A: 

Try this-

$('input[type="checkbox"]').click(function(){
  if($(this).is(':checked')){
     var val = $(this).val();
     $('.hidden-filters > ul').append('<li>'+val+'</li>');
  } 
});
Alpesh
+1  A: 
$(":checkbox").click(function(){
    $(".hidden-filters").append("<li>" + $(this).val() + "</li>");
});
hunter
@Hunter, this works perfectly the only thing that I saw is that if you keep on clicking on the checkbox, it would continue populating the checkbox value. Any advice on this. Thanks.
Ozzy
@Ozzy, have a look at my answer, though you'll have to fill in the requisite element names yourself.
David Thomas
+3  A: 

One way of doing this, but note I've not covered how to remove the inserted li elements from the list when/if they're unchecked (See after the hr):

$(document).ready(
    function(){
        $('input:checkbox').change(
            function(){
                if ($(this).is(':checked')) {
                 $('<li />').appendTo('#div ul').text($(this).val());   
                }
            }); 
    });

Demo at JS Fiddle.


Edited to offer a removal facility, if the checkbox is unchecked:

$(document).ready(
    function(){
        $('input:checkbox').change(
            function(){
                if ($(this).is(':checked')) {
                 $('<li />').appendTo('#div ul').text($(this).val());   
                }
                else {
                    $('#div li:contains('+$(this).val()+')').remove();
                }
            }); 
    });

Demo at JS Fiddle.

David Thomas
Thanks for this Thomas it works wonderful.
Ozzy
No, *thank you* for the accept; and always glad to be of help =)
David Thomas