?
Solved

Jquery append value from the checked checkboxes

Posted on 2011-09-02
5
Medium Priority
?
331 Views
Last Modified: 2012-05-12
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

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
  • 3
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36477194
And what is the problem?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 36477832
The problem is how do I remove those values from the list when the ckeckbox in unchecked
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36478632
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(", "));
})
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 36482799
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().

see:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

Also refer this for other manipulation with javascript array.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array

Hope this helps you and get out of your small problem. :-)
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 36482996
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() {
          getCheck(false);
        }
      }
      if (inp.checked) vals.push(inp.value)
    }
  }
  document.getElementById("bodyIDList").value=vals.length>0?vals.join(", "):"";
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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