Skip to content
Advertisement

Copy Selected Checkbox Value From One Checkbox To Another Immediately Using JQUERY

I’m relatively new to Jquery and have been struggling with this problem for the last day or so. I am trying to do something seemingly simple, but I can’t quite work it out. I have two identical checkboxes with many entries and multiple choices can be selected by the end user in either checkbox. If a value in checkboxa is selected, I want it to immediately select the identical value in the checkboxb. I have researched many solutions, and I have tried the following:

JavaScript

My HTML. I’m using Django template and forms with a queryset for the choices, which is why I am trying to leverage the ID via Jquery for checkbox manipulation.

JavaScript

I found the approach above by referencing the following code snippet on SO..Copy check state of a set of checkboxes to another fieldset

The above will copy the first value I select in checkboxa to the top of the checkboxb, but that’s it. I suspect I need to loop through the index or create an array to try and find the checked values, but can’t quite work it out. I definitely want to use the JQUERY ID approach in solving this problem. Thanks in advance for any thoughts.

After experimenting, I have gotten as far as…

JavaScript

And my console will show the hello world!

When I try to do something like…

JavaScript

I get the following error:

JavaScript

I know from prior experimentation that Django forms and JQuery can be a challenge to connect just right at times. Thanks in advance for any other thoughts.

If I do the following…

JavaScript

The minute I select a checkbox value in checkboxa, all of checkboxb values get selected. I am trying to just get the value I selected in checkboxa to appear n checkboxb, not all checkboxes.

Advertisement

Answer

I believe I know what you are trying to do. This seems like it could be easier with classes (group the checkboxes by class names)

When a checkbox with checkboxA class is clicked it will also check the checkbox with class checkboxB and only if it also has the same value as checkboxA

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement