Select a row using JQuery
I have an HTML table as shown below:
ColA ColB ColC ColD ColE ColF
Checked AAAA BBBB CCCC DDDD EEEE
Unchecked AAAA BBBB CCCC DDDD EEEE
Checked AAAA BBBB CCCC DDDD EEEE
Checked AAAA BBBB CCCC DDDD EEEE
Unchecked AAAA BBBB CCCC DDDD EEEE
Checked AAAA BBBB CCCC DDDD EEEE
Checked AAAA BBBB CCCC DDDD EEEE
ColA is a checkbox. I want to get ColD value for all rows whose Cola is checked. I want to use jquery for this. Has anyone met before?
Best wishes,
+2
a source to share
3 answers
No-jQuery solution
var inputs = document.getElementById("tableId").getElementsByTagName("input"), input, i = inputs.length;
while (i--){
input = inputs[i];
if (input.type == "checkbox" && input.checked){
console.log(input.parentNode.parentNode.childNodes[3].innerHTML);
}
}
This is guaranteed to be much faster than any of the css selector methods (and easier to understand).
And rewrite a little to return an array
var array_of_values = (function(table){
var values = [], inputs = table.getElementsByTagName("input"), i = inputs.length;
while (i--)
if (inputs[i].type == "checkbox" && inputs[i].checked)
values.push(inputs[i].parentNode.parentNode.childNodes[3].innerHTML);
return values;
})(document.getElementById("tableId"));
0
a source to share