[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Disable/Enable checkboxes using jQuery and special cases

Posted on 2011-10-17
5
Medium Priority
?
218 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:dolythgoe
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:saimazz
ID: 36981376
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

0
 

Author Comment

by:dolythgoe
ID: 36986810
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.
0
 
LVL 3

Accepted Solution

by:
saimazz earned 2000 total points
ID: 36987334
this shoul resolve your exeptions:

for group_id list that splits the value and check, and i also insert additional check for checked elements

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.attr('checked') != 'checked') {
				self.removeAttr("checked");
			}
		})

Open in new window

0
 

Author Comment

by:dolythgoe
ID: 36994802
Thanks a lot for this :D
0
 
LVL 3

Expert Comment

by:saimazz
ID: 36994825
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question