• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

jquery unchecked checkbox

I have a list of checkboxes. Upon clicking on each of the checkboxes i am adding the value to the hidden variable. But the question is if I want to remove the value from the list upon unchecking the checkbox . How this piece cab be done

here is the hidden form variable<input name="IDList[]" type="hidden" id="IDList" value="" /> and the jquery $(".myCheckboxClass").change(function() { var output = 0; $(".myCheckboxClass").change(function() {       if( $(this).is(":checked") ) { output += ", " + $(this).val();       }else {       output = $.grep(output, function(value) {       return value != $(this).val();       });       }       $("#IDList").val(output ); })
0
erikTsomik
Asked:
erikTsomik
  • 7
  • 6
1 Solution
 
leakim971PluritechnicianCommented:
Recreate the list each time instead adding/removing element. If one of the checkboxes change (your current selector), loop over all elements (samedi selector) check if checked or unchecked and add to the list only if checked
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
well i did figure out how to add it but how to remove it from there  
0
 
leakim971PluritechnicianCommented:
Clear it
Rebuild it each click looping over all checkboxes not only the clicked
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
erikTsomikSystem Architect, CF programmer Author Commented:
can you provie the piece of code for it . here is my code the way I am adding values to the list.
 var output = 0;
 if( $(this).is(":checked") ) {
    output += ", " + $(this).val();
                                                                        
                                                                        
 }else {      
      delete output[$(this).val()];                                                                        }                                     
                                                                     $("#IDList").val(output );
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I just chnage my delete
 to this
      var getList = $("#bodyIDList").val();
                                                                              $.each( getList, function(intIndex,objValue) {
                                                                                    alert(objValue);
                                                                              }
                                                                              
                                                                              )

And When i do the alert my alert  show each individual items separately li ke 1 2 all comma and so on
0
 
leakim971PluritechnicianCommented:
check this :
	$(".myCheckboxClass").change(function() {
		var output = []; // clear it
		$(".myCheckboxClass").each(function(i, el) {
			if( $(this).is(":checked") ) {
				output.push( $(this).val() );
			}
		})
		output.join(","); // add the comma and build the string 
		$("#IDList").val(output);
	})

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
will this take care of unchecking the checkbox and removing item from the list
0
 
leakim971PluritechnicianCommented:
try it and please confirm it run fine for you
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
Unfortunately , it does not run fine , it adding extra values , and when i uncheck it also adding values rather than removing them. It create a copy of the previously selected elements . I thing we do not need each loop there
0
 
leakim971PluritechnicianCommented:
as you can see we have a << if( $(this).is(":checked") ) { >>
mean we rebuild each time the string only with checked value
line 2 reset the string : var output = []; // clear it
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
in my login it will reset every time  i am searching for a new name so it has to be outside
0
 
leakim971PluritechnicianCommented:
>in my login it will reset every time  i am searching for a new name so it has to be outside

sorry I ddon't understand
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
can you tell me how can I do accomplish my logic here. Thank you in advance
0
 
Gurvinder Pal SinghCommented:
I guess this was your first question on the same problem on which you now have three open question now :)

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27291333.html
#36493631

Let me know if anything is pending.

@leakim971 gave the answer first so he deserves the points here
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now