LimMH
asked on
Find ID and Values of Checkboxes from a form
Hi,
I have a form which I populated with checkboxes having ID based on the records pulled from a table.
eg: Table contains:
ID, Name
30, Some thing
64, Some one
28, Some where
70, Some how
By means of pulling info from the table, the eventual form will be:
<form>
<input type='checkbox' id='30' / >Some thing
<input type='checkbox' id='64' / >Some one
<input type='checkbox' id='28' / >Some where
<input type='checkbox' id='70' / >Some how
<input type='button' onclick='getcheckboxinfo() ' />
</form>
Note: if there are more records, there will be more checkboxes. Thus no of checkboxes and their respective ID are unknown.
In the javascript function, how do I get the ID's of all checkboxes and their checked status?
If it's possible, having the answer in jquery will be best.
I know that from jquery I can get the number of checkboxes from:
var cbs = $("form input:checkbox"); //find all checkboxes
var nbCbs = cbs.size(); //the number of checkboxes
But I need help to get the ID values and the checkstatus of each and every checkboxes.
Thanks
I have a form which I populated with checkboxes having ID based on the records pulled from a table.
eg: Table contains:
ID, Name
30, Some thing
64, Some one
28, Some where
70, Some how
By means of pulling info from the table, the eventual form will be:
<form>
<input type='checkbox' id='30' / >Some thing
<input type='checkbox' id='64' / >Some one
<input type='checkbox' id='28' / >Some where
<input type='checkbox' id='70' / >Some how
<input type='button' onclick='getcheckboxinfo()
</form>
Note: if there are more records, there will be more checkboxes. Thus no of checkboxes and their respective ID are unknown.
In the javascript function, how do I get the ID's of all checkboxes and their checked status?
If it's possible, having the answer in jquery will be best.
I know that from jquery I can get the number of checkboxes from:
var cbs = $("form input:checkbox"); //find all checkboxes
var nbCbs = cbs.size(); //the number of checkboxes
But I need help to get the ID values and the checkstatus of each and every checkboxes.
Thanks
Here it is in jQuery
//Holds the checkboxs as id=>checked
var chbs = [];
$("form input:checkbox").each(function(){
chbs[this.id] = this.checked;
});
//Or as one array of ids and one of values
var chbsIDs = [];
var chbsVals = [];
$("form input:checkbox").each(function(){
chbsIDs.push(this.id);
chbsVals.push(this.checked);
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for writting in detail. I am so new to jquery, didn't know how to code it. Got a new understanding of it now.
ASKER
var inputs = document.getElementsByTagN
var cbsx = []; //will contain all checkboxes checked status
var cbsid = []; //will contain all checkboxes ID values
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (inputs[i].name!=""){
cbsx.push(inputs[i].checke
cbsid.push(inputs[i].name)
}
}
}
Anyone can get it using jquery?