Solved

jquery unchecked checkbox

Posted on 2011-09-02
14
717 Views
Last Modified: 2012-05-12
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
Comment
Question by:erikTsomik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36475538
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36475549
well i did figure out how to add it but how to remove it from there  
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36475560
Clear it
Rebuild it each click looping over all checkboxes not only the clicked
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 19

Author Comment

by:erikTsomik
ID: 36475880
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36476149
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36477852
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36477916
will this take care of unchecking the checkbox and removing item from the list
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36478141
try it and please confirm it run fine for you
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 36478824
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
 
LVL 82

Expert Comment

by:leakim971
ID: 36479024
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36479369
in my login it will reset every time  i am searching for a new name so it has to be outside
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36480441
>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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36481884
can you tell me how can I do accomplish my logic here. Thank you in advance
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36500695
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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