Link to home
Start Free TrialLog in
Avatar of dolythgoe
dolythgoe

asked on

Disable/Enable checkboxes using jQuery and special cases

Hi all,

I have this code working well (thanks to leakim):

$.disableFilter = function(queryString)
{
	var url = "getTags.php" + queryString;
	$.get(url, function(filterdata) {
		
		var arr = filterdata.split(",");
		$(":checkbox", "#filter_column").each(function() {
			if( $.inArray($(this).attr("id"), arr) == -1 ) {
				$(this).attr("disabled", true);
			}
			else {
				$(this).attr("disabled", false);
			}
		})

	});
}

Open in new window


This ajax call returns a set of values after sending a set of values via queryString variable.

Now, I have a load of checkboxes:

<input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="25-436 />
<input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="25-437 />
<input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="25-438 />
<input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="26-449 />
...more...

Open in new window


The value is split into 2, first part being the group_id and the second being the option_id

The jquery code above is used to disable any checkboxes that are NOT in the returned string from the AJAX call.

I need to amend this code based on value as opposed to id and extend it to do the following.

- Depending on the group_id in the first part of the queryString var, only make those appliceable for disabling that do not belong to this group.
- Do not disable any checkboxes already checked

An example of queryString var might be:

filter_options=1-1,1-2,1-5,2-6,2-10,3-15,3-16

([group_id]-[option-id])

Return value is just option_ids which are appliceable for further filtering: 16,17,18,19 etc..

So I need to adjust my code to:

- Extract the group_id from the first value in queryString
- Run trhough the checkboxes and disable those not in the returned value (except those that belong to the first group_id and those that are already selected)

Hope that makes sense, bit hard to explain!

huge thanks
Avatar of saimazz
saimazz

how about this:

var array = option_ids.split(",");
var belongs = 0;
		$(":checkbox", "#filter_column").each(function() {
		var self = $(this);		
		var value = self.val().split('-');	
		if($.inArray(value[0], array) == -1) {
				self.removeAttr("checked");
			}
		})

Open in new window

Avatar of dolythgoe

ASKER

Hey, thanks for that.

What's the var belongs? This code appears to work except it doesn't meet the 2 conditions:


- ignore those that belong to the first group_id of queryString
- ignore those that are already selected

Any further help is much appreciated.
ASKER CERTIFIED SOLUTION
Avatar of saimazz
saimazz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for this :D
by the way your asked what means that var :

var self = $(this);

Open in new window


is for caching purpose, so then you dont need to request DOM with $(this)...  several times.