Reputation: 25
I need to create an array of the id's from each <input type="checkbox">
in a selected div.
HTML
<div id="ADC-Designer-CAD">
<table>
<tr>
<td>Teamcenter Completed:</td>
<td><input type="checkbox" id="tc" onChange="mtcb()" /></td>
<td><input type="button" id="tc" class="btn btn-default"onClick="ScrollToKP();" /></td>
</tr>
<tr>
<td>NX Manager Completed:</td>
<td><input type="checkbox" id="nxm" onChange="mtcb()" /></td>
<td><input type="button" id="nxm" class="btn btn-default"onClick="ScrollToKP();" /></td>
</tr>
</table>
</div>
There are multiple div's like this one with different id's to them. My code selects the div first. I can get it to filter the correct div. I can't get it to create the array of the check box id's inside that div.
JavaScript
$.get('content/page.php', function(data) {
data1 = $(data).filter('#' + actSite + '-' + actUT + '-CAD');
data2 = $(data1).filter(`*Not sure what to put here*`);
});
data1
works as it should and gets from <table>...</table>
I need something to end looking like
data1 = ["tc", "nxm", ...]
If I were to create an array for each item I need an array for, it would take weeks. I need to be able to change the data in data1
over and over again.
EDIT / SOLUTION - 4/21/2015
I have changed all my ID's for the checkboxes as many have commented. Here is my WORKING code:
var NewArray = [];
$(':checkbox').each(function(index, element) {
NewArray.push(this.id);
});
//results NewArray = [ADC-Designer-CAD-tc,ADC-Designer-CAD-nxm,....]
Upvotes: 1
Views: 2296
Reputation: 7146
Are you sure you want to use JQuery ?
I think this might do the job :
function getIDs() {
var inputs = document.getElementsByTagName("input")
var i;
var checkboxes = [];
for (i = 0 ; i < inputs.length ; i++) {
if (inputs[i].getAttribute("type") == "checkbox")
checkboxes.push(inputs[i].id);
}
return checkboxes;
}
Basicaly, I search in all input
elements which ones has the checkbox
type. Then, I push the ID in an array.
And I highly recommend to have only ONE element with an ID. Having two elements with the same ID is bad.
That's why we have getElementById()
with no 's'
and getElementsByClassName()
with an 's'
Upvotes: 1
Reputation: 1082
Try javascript like this:
var result = new Array();
$("#ADC-Designer-CAD input[type=checkbox]").each(function(index, element) {
result.push($(element).attr("id"));
});
Upvotes: 1