1

Im very, very new to jquery. Im trying to do something that most likely seems elementary to most of the gurus here, but I think I have been messing with it for so long, that I must be overlooking something very simple.

I have a large list of checkboxes (85 in total) that need to be checked based on the value selected in a select list. Both the select list and the checkboxes are created dynamically via an ASP page/database query.

Link to fiddle: http://jsfiddle.net/v9p5C/21/

HTML:

<select id="roleID">
    <option data-filter=''>ID</option>
    <option data-filter='200'>200</option>
    <option data-filter='300'>300</option>
    <option data-filter='400'>400</option>
</select>
<BR>
<BR>
<input type="checkbox" data-tags='["200"]'>Create
<br>
<input type="checkbox" data-tags='["200,300"]'>Edit
<br>
<input type="checkbox" data-tags='["200","300, 400"]'>View
<br>
<input type="checkbox" data-tags='["200"]'>Delete
<br>
<input type="checkbox" data-tags='["200"]'>Archive
<br>

SCRIPT:

$(function () {
    $(document).on('change', function (e) {

        var filter = $(this).data('filter');

        $('input[type="checkbox"]').each(function () {
            var $checkbox = $(this);
            var tags = $checkbox.data('tags');
            $.each(tags, function () {
                if (filter == this) $checkbox.prop("checked", !$checkbox.prop("checked"));
            });
        });

    });
});
1
  • I recommend not using the quotes around the data-filter options because you've mixed up a couple of brackets. Commented Aug 12, 2014 at 21:55

2 Answers 2

2

Your jQuery will need to look more like this:

$(function () {
    $('#roleID').on('change', function (e) {

        var filter = $(this).val();

        $('input[type="checkbox"]').removeAttr('checked');
        $('input[type="checkbox"][data-tags*="'+filter+'"]').attr('checked','checked');

    });
});

This is a very simple way of creating that functionality. You can view the fiddle here:

http://jsfiddle.net/m0nk3y/kjnoovL0/1/

Sign up to request clarification or add additional context in comments.

8 Comments

There's room for error in this setup, depending on the values you're checking for. I used the CSS3 selector ([attr*=value]) because it's much easier for newbies, I assume. However, if you have more complex values to check for, you may want to use indexOf() or something similar. Good luck.
turns out I need to change this somewhat. New to stackoverflow as well, so I am not sure how to follow up. I guess I need to slightly alter what I am doing as I need to have a load defaults checkbox instead. I kind of have it working in this fiddle (jsfiddle.net/kjnoovL0/5), but the checkbox isnt staying checked.
What is the load defaults checkbox supposed to do?
I noticed that you forgot to put quotes around roleID. var filter = $('#roleID').val();
basically, only if the loadDEfaults checkbox is checked, the corresponding options get checked. This is what I am playing with now: jsfiddle.net/kjnoovL0/17.
|
2

the var filter = $(this).data('filter'); line was attempting to get the data-filter value from the select element, not the selected option element. Here's how you would grab the selection option's data-filter:

var filter = $('option:selected', this).data('filter');

updated fiddle:

http://jsfiddle.net/v9p5C/52/

(there were a couple of other minor bugs in the fiddle that are also fixed)

3 Comments

Problem with this method is that if he checks something like 200 after checking 300, the two middle ones will not be selected.
jsfiddle.net/v9p5C/54 I just updated your fiddle here and there. I also changed the HTML data-filter options to not include quotes. Since these are numbers, they aren't really necessary.
agree, I was trying to keep it close to the OP's fiddle

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.