I've this HTML table:
<table class="table table-condensed">
<thead>
<tr>
<th><input type="checkbox" id="toggleCheckboxSelFabricante" name="toggleCheckboxSelFabricante"></th>
<th>Fabricante</th>
</tr>
</thead>
<tbody id="selFabricanteBody">
<tr>
<td><input type="checkbox" name="selChkFabricante" id="selChkFabricante3" value="3"></td>
<td>Eos est ipsam.</td>
</tr>
</tbody>
</table>
I need to create a key => value for manufacturerColl var where id is the value of each checked checkbox (first td on the table) and name is the text on the second column but don't know how to. This is what I've in my code:
var checkedModelBranch = $("#parMarcaModeloFabricanteBody").find("input[type='checkbox']:checked"),
checkedManufacturers = $("#selFabricanteBody").find("input[type='checkbox']:checked"),
manufacturerColl = [],
modelBranchManufacturerCollection;
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push(checkedManufacturers[j].value);
}
for (var i = 0; i < checkedModelBranch.length; i++) {
modelBranchManufacturerCollection = addNewRelationModelBranchManufacturer(checkedModelBranch[i].value, manufacturerColl);
if (modelBranchManufacturerCollection) {
for (var k = 0; k < modelBranchManufacturerCollection.manufacturerKeyCollection.length; k++) {
$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).html(modelBranchManufacturerCollection.manufacturerKeyCollection[k] + '<br/>');
}
}
}
What I need in others words is for each manufacturerColl have and id => name, ex:
manufacturerColl[] = {
id: someId,
name: someName
};
And I'm not sure but maybe this could work:
// move foreach selected checkbox and get the needed
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push({
id: checkedManufacturers[j].value,
name: "" // how do I get the value on the second column?
});
}
Which is the right way to do this? How do I get the value on the second column on each iteration?
Approach
I don't know if this is complete right but is what I've done and it's buggy. See the code:
var checkedModelBranch = $("#parMarcaModeloFabricanteBody").find("input[type='checkbox']:checked"),
checkedManufacturers = $("#selFabricanteBody").find("input[type='checkbox']:checked"),
// I added this var to get all the names
checkedManufacturersName = $("#selFabricanteBody").find("input[type='checkbox']:checked").parent().next('td').text(),
manufacturerColl = [],
modelBranchManufacturerCollection;
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push({
id: checkedManufacturers[j].value,
// Here I'm tying to put the entire name but get only the first character
name: checkedManufacturersName[j]
});
}
for (var i = 0; i < checkedModelBranch.length; i++) {
modelBranchManufacturerCollection = addNewRelationModelBranchManufacturer(checkedModelBranch[i].value, manufacturerColl);
if (modelBranchManufacturerCollection) {
//$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).siblings().attr("rowspan", modelBranchManufacturerCollection.manufacturerKeyCollection.length);
for (var k = 0; k < modelBranchManufacturerCollection.manufacturerKeyCollection.length; k++) {
// then I render the name attribute from the collection
$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).append((modelBranchManufacturerCollection.manufacturerKeyCollection)[k].name + '<br/>');
}
}
}
Why I'm inserting/getting the first character only and not the complete string?
['text1', 'text2']where the array indices are the checkbox values (sparse arrays are one of the few things javascript handles well) and when you iterate through the checked checkboxes just pull the 'name' values from that array.