0

I can't seem to figure out what i'm doing wrong with the following code:

            $('#side_categories #sourcestoggle ul#side_categories_prog .showonly').click(function () {
        var cattest2 = $(this).data('dealcat'); //get the category from the clicked button
        $('#listings .deal:not(:has(.deal[data-dealcat*="' +cattest2+ '"]))').addClass('filtered');
        $('#sourcestoggle ul#side_categories_prog li .check_box input:not([data-dealcat*="' +cattest2+ '"]').removeAttr('checked');
        $('#listings .deal[data-dealcat*="' +cattest2+ '"]').removeClass('filtered');
        $('#dealcount .counter #showingval').html(parseInt($('#listings .deal:visible').size()));
        });

it works to show ONLY the selected category based on data-dealcat, however it also unchecks all checkboxes, I can't seem to figure out how to keep the current checkbox checked. Since I'm new to jquery (and coding in general), i'm pretty certain the code is fairly sloppy but it still works for the most part, just the checkbox which has the "only" link on it doesn't stay checked.

Thank you all in advance!

edit: the above code now keeps the first checked box, but when u click the others it doesn't do anything.

1
  • What I meant by "doesn't do anything" meaning the checkboxes dont change, the function do, for instance, source1 [only] is clicked, all others will be hidden and source 1 will keep its checkbox. should you click on source2 [only] after, it changes the displayed listings properly, but all checkboxes are cleared including source2. Commented Sep 6, 2011 at 21:22

1 Answer 1

1

Try this

$('#side_categories #sourcestoggle ul#side_categories_prog .showonly').click(function () {
        var cattest2 = $(this).data('dealcat'); //get the category from the clicked button
        var checked = $('#sourcestoggle ul#side_categories_prog li .check_box input.deal[data-dealcat*="' +cattest2+ '"]').is(':checked');

        $('#listings .deal:not(:has(.deal[data-dealcat*="' +cattest2+ '"]))').addClass('filtered');

        $('#sourcestoggle ul#side_categories_prog li .check_box input:not(.deal[data-dealcat*="' +cattest2+ '"])').removeAttr('checked');

        $('#sourcestoggle ul#side_categories_prog li .check_box input.deal[data-dealcat*="' +cattest2+ '"]').attr('checked', checked);

        $('#listings .deal[data-dealcat*="' +cattest2+ '"]').removeClass('filtered');
        $('#dealcount .counter #showingval').html(parseInt($('#listings .deal:visible').size()));
        });
Sign up to request clarification or add additional context in comments.

2 Comments

on the first click, first source remains checked, but should others be clicked, the listings are filtered properly, but no changes are made to the checkboxes. I also had to make a few changes to remove .deal from the inputs since that was wrong when i initially pasted it.
I changed .attr('checked', checked); -> .attr('checked', +checked); and it works!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.