erikTsomik
asked on
get a list of selected checkboxes using Jquery
I have the set of checkboxes Upon clicking on the checkbox
I need to show selected checkbox.
My code is below
$(".myCheckboxClass").chan ge(functio n() {
var list = $('#IDList').data('value') ;
if( $(this).is(":checked") ) {
list.push( $(this).val());
//$("#selectedListSelected ").append( '<div>');
$(this).closest("div").clo ne().appen dTo("#sele ctedListSe lected");
} else {
var index = list.indexOf($(this).val() );
list.splice(index, 1);
}
$("#IDList").val(list);
})
I need to show selected checkbox.
My code is below
$(".myCheckboxClass").chan
var list = $('#IDList').data('value')
if( $(this).is(":checked") ) {
list.push( $(this).val());
//$("#selectedListSelected
$(this).closest("div").clo
} else {
var index = list.indexOf($(this).val()
list.splice(index, 1);
}
$("#IDList").val(list);
})
Seems related to other posts.
ASKER
well the problem is when i check 1 checkbox all the checkboxes get clonned and I only need need the one that checked
So what is wrong with my suggestion in the other question and why do you repeat the question?
I suggested you get all checkboxes and replace the content with the checked ones each time
I suggested you get all checkboxes and replace the content with the checked ones each time
ASKER
well there was no answer to that question. I will be deleteting old question.
ASKER
All I need to copy is the text that comes after a checkbox . some very similar to this http://odyniec.net/articles/multiple-select-fields/ when you will see selected items with ability to remove them
I suggested this in the other question.
$(".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.
})
ASKER
this my entire code. As you can see the value of the checkbox is a numeric value and I need the text after it
for (var i = 0; i < data.recordcount; i++) {
$("#selectedList").append( '<div>');
$("#selectedList").append( '<label></ label>');
$("#selectedList").append( '<input type=checkbox name=body class="myCheckboxClass" value=' + data.data.bodyid + '>');
$("#selectedList").append( data.data. bodydata); $("#selectedList").append( '<br>');
$("#selectedList").append( '</div>');
}
$(".myCheckboxClass").chan ge(functio n() {
var list = $('#bodyIDList').data('val ue');
if( $(this).is(":checked") ) {
list.push( $(this).val());
$(this).closest("checkbox" ).clone(). appendTo(" #selectedL istSelecte d");
} else {
var index = list.indexOf($(this).val() );
list.splice(index, 1);
}
$("#bodyIDList").val(list) ;
})
for (var i = 0; i < data.recordcount; i++) {
$("#selectedList").append(
$("#selectedList").append(
$("#selectedList").append(
$("#selectedList").append(
$("#selectedList").append(
}
$(".myCheckboxClass").chan
var list = $('#bodyIDList').data('val
if( $(this).is(":checked") ) {
list.push( $(this).val());
$(this).closest("checkbox"
} else {
var index = list.indexOf($(this).val()
list.splice(index, 1);
}
$("#bodyIDList").val(list)
})
ASKER
is there any updates on this issue
I am in CET and will not be online for another hoour and a half. What is data.data ?
ASKER
data.data is comming from my ajax call . Ajax return JSON which is the values that i need to fill the required data
ASKER
Is there any updates?
ASKER
I have updated the code so it is does show me the list of the selected items. Now the challange is when i do the uncheck along with removal of ID which is happening I need to remove the block from the listing
if( $(this).is(":checked") ) {
list.push( $(this).val());
var text = $('label[for=' + $(this).val() + ']').text();
//alert(text);
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li onclick="this.parentNode.r emoveChild (this);">' + text + '</li></ul>');
} else {
var index = list.indexOf($(this).val() );
list.splice(index, 1);
//$("#selectedListSelected li").parent().remove(this) ;
$(this).closest('ul').remo ve();
}
if( $(this).is(":checked") ) {
list.push( $(this).val());
var text = $('label[for=' + $(this).val() + ']').text();
//alert(text);
$("#selectedListSelected")
$("#selectedListSelected")
} else {
var index = list.indexOf($(this).val()
list.splice(index, 1);
//$("#selectedListSelected
$(this).closest('ul').remo
}
I guess you need to run this code for all checkboxes and only show those which have been selected.
Basically, you should refresh this 'selectedListSelected' everytime you check or uncheck the checkbox
Basically, you should refresh this 'selectedListSelected' everytime you check or uncheck the checkbox
ASKER
can you update my code . how can I do that
just assign the onclick event to all the checkbox, and at onclick event iterate through all the checkboxes
mplungian has already suggested that here #36484323
$(".myCheckboxClass").clic k(function () {
var vals = [];
$(".myCheckboxClass:checke d").each(f unction() {
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li onclick="this.parentNode.r emoveChild (this);">' + text + '</li></ul>');
}
})
mplungian has already suggested that here #36484323
$(".myCheckboxClass").clic
var vals = [];
$(".myCheckboxClass:checke
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
})
ASKER
i cannot use each because it will also add duplicate items to the running list
it won't, just clear that div before running each
$(".myCheckboxClass").clic k(function () {
$("#selectedListSelected") .html("");
$(".myCheckboxClass:checke d").each(f unction() {
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li onclick="this.parentNode.r emoveChild (this);">' + text + '</li></ul>');
}
})
$(".myCheckboxClass").clic
$("#selectedListSelected")
$(".myCheckboxClass:checke
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
})
ASKER
when I uncheck the checkbox it does not remove the item form the selectedListSelected
if this is not working check this
$(".myCheckboxClass").clic k(function () {
$("#selectedListSelected") .html("");
$(".myCheckboxClass").each (function( ) {
if ( $(this).is(':checked') )
{
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li onclick="this.parentNode.r emoveChild (this);">' + text + '</li></ul>');
}
});
});
$(".myCheckboxClass").clic
$("#selectedListSelected")
$(".myCheckboxClass").each
if ( $(this).is(':checked') )
{
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
});
});
ASKER
Thanks that great. Now as you can see the last line have onclikc event
$("#selectedListSelected") .append('< ul class=options><li onclick="this.parentNode.r emoveChild (this);">' + text + '</li></ul>');
When this is clicked it does remove the item from there But I also need to update my running list and remove the value from there
$("#selectedListSelected")
When this is clicked it does remove the item from there But I also need to update my running list and remove the value from there
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how can i also include the label to be removed. also upon removal i need to remove id from the running list
for removing label, make it
$(".myCheckboxClass").clic k(function () {
$("#selectedListSelected") .html("");
$(".myCheckboxClass").each (function( ) {
if ( $(this).is(':checked') )
{
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li>' + text + '</li></ul>');
}
});
$(".options li").bind("click", function(){
e.preventDefault();
$(this).parent().prev("lab el").remov e(); \\ or just $(this).parent().prev().re move();
$(this).remove();
return false;
});
});
$(".myCheckboxClass").clic
$("#selectedListSelected")
$(".myCheckboxClass").each
if ( $(this).is(':checked') )
{
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
});
$(".options li").bind("click", function(){
e.preventDefault();
$(this).parent().prev("lab
$(this).remove();
return false;
});
});
ASKER
how can I update the running total? Will unchecking the appropriat checkbox will do the trick
<< how can I update the running total?>>
Elaborate on it more....Or better please post a separate question for the same
Elaborate on it more....Or better please post a separate question for the same
ASKER
if you look at my previous comments var list = $('#bodyIDList').data('val ue');
bodyIDList is running total
bodyIDList is running total
$(".myCheckboxClass").clic
$("#selectedListSelected")
var total = 0;
$(".myCheckboxClass").each
if ( $(this).is(':checked') )
{
total += parseInt( $(this).val() );
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
});
$(".options li").bind("click", function(){
e.preventDefault();
$(this).parent().prev("lab
$(this).remove();
return false;
});
$("#bodyIDList").val(total
});
ASKER
this is not a total this is simply a list of checkboxes value
then make it
$(".myCheckboxClass").clic k(function () {
$("#selectedListSelected") .html("");
var total = "";
$(".myCheckboxClass").each (function( ) {
if ( $(this).is(':checked') )
{
total += $(this).val() + ",";
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected") .append('< label></la bel>');
$("#selectedListSelected") .append('< ul class=options><li>' + text + '</li></ul>');
}
});
$(".options li").bind("click", function(){
e.preventDefault();
$(this).parent().prev("lab el").remov e(); \\ or just $(this).parent().prev().re move();
$(this).remove();
return false;
});
$("#bodyIDList").val(total .substring (0, (total.length - 1) ) );
});
$(".myCheckboxClass").clic
$("#selectedListSelected")
var total = "";
$(".myCheckboxClass").each
if ( $(this).is(':checked') )
{
total += $(this).val() + ",";
var text = $('label[for=' + $(this).val() + ']').text();
$("#selectedListSelected")
$("#selectedListSelected")
}
});
$(".options li").bind("click", function(){
e.preventDefault();
$(this).parent().prev("lab
$(this).remove();
return false;
});
$("#bodyIDList").val(total
});
ASKER
how can i uncheck the chckbox for removed item. SO if click on remove in need to uncheck the checkbox.
i added the id to checkbox
can i do something like this $("#" + $(this).val()).removeAttr( "checked") ;
i added the id to checkbox
can i do something like this $("#" + $(this).val()).removeAttr(
@erikTsomik: Don't mind but your question is taking different turns now...Please open a new question for new problems
ASKER
I forgot to mention that the main checkboxes are driven by my search routine. So now If I search by new name my listing get cleared because I am clearing it evrytime . If I remove the clearing than than when i uncheck the checkbox I can not remove the particular item
Is there anything pending now? Is your issue resolved?
function showSelectedCheckboxes()
{
var list = $('#IDList').data('value')
$(".myCheckboxClass:checkb
list.push( $(this).val());
$(this).closest("div").clo
});
$("#IDList").val(list);
}