erikTsomik
asked on
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 = '';
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 );
})
And what is the problem?
ASKER
The problem is how do I remove those values from the list when the ckeckbox in unchecked
Try something like this (not tested)
$(".myCheckboxClass").clic k(function () {
var vals = [];
$(".myCheckboxClass").each (function( ) {
if ($(this).is(':checked') vals.push($(this).val());
}
$("#bodyIDList").val(vals. join(", "));
})
$(".myCheckboxClass").clic
var vals = [];
$(".myCheckboxClass").each
if ($(this).is(':checked') vals.push($(this).val());
}
$("#bodyIDList").val(vals.
})
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. :-)
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. :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.