Mike
Mike

Reputation: 463

jquery not selector with .data

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.

Upvotes: 0

Views: 1327

Answers (1)

ShankarSangoli
ShankarSangoli

Reputation: 69915

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

Upvotes: 1

Related Questions