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

Jquery append value from the checked checkboxes

I my app i have search textbox .When I type something in will produce the list
of names along with checkboxes. upon clicking on the checkbox I want
to append the value of the checkbox to the hidden form variable.

As i type second time I I get a different list it add more options .

My code is here
 var output = '';
$(".myCheckboxClass").change(function() {
   											 $('input[type="checkbox"]:checked').each(function(index) {
    output += $(this).val() + ", ";

   $("#bodyIDList").val(output );

Open in new window

  • 3
1 Solution
Michel PlungjanIT ExpertCommented:
And what is the problem?
erikTsomikSystem Architect, CF programmer Author Commented:
The problem is how do I remove those values from the list when the ckeckbox in unchecked
Michel PlungjanIT ExpertCommented:
Try something like this (not tested)

$(".myCheckboxClass").click(function() {
  var vals = [];
  $(".myCheckboxClass").each(function() {
    if ($(this).is(':checked') vals.push($(this).val());
  $("#bodyIDList").val(vals.join(", "));
Better way use javascript.
Just call function on click of checkbox.

In that function take an array (like in javascript). Then first check if that string (checkbox value) is already in array or not by using indexOf().



Also refer this for other manipulation with javascript array.


Hope this helps you and get out of your small problem. :-)
Michel PlungjanIT ExpertCommented:
jQuery IS javascript. If you already use it, it is in most cases making your life a lot easier, assuming you can get to grips with the syntax.

The jQuery I posted would look like this in plain JS

window.onload=function() {
  getCheck(true); // 
function getCheck(isInit) {
  var inputs = document.getElementsByTagName("input");
  var vals = [];
  var inp;
  for (var i=0, n=inputs.length;i<n;i++) {
    inp = inputs[i]; 
    if (inp.className.indexOf("myCheckboxClass") !=-1) {
      if (isInit) {
        inp.onclick=function() {
      if (inp.checked) vals.push(inp.value)
  document.getElementById("bodyIDList").value=vals.length>0?vals.join(", "):"";

Open in new window

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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