I have a table such as this below:
<table id="Grid">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td class="k-dirty-cell">$100</td>
</tr>
<tr>
<td>February</td>
<td class="k-dirty-cell">$80</td>
</tr>
<tr>
<td>March</td>
<td>$98</td>
</tr>
</tbody>
</table>
I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not.
The class is always in the second column, so I am only interested in iterating through the rows of the second column - i.e., when cellIndex = 2.
I have tried this so far but I'm a bit stuck:
$("#Grid tbody").find('td').each(
function () {
debugger;
// run for specific columns - where validation is needed
// var isDirty = cellToValidate.hasClass('k-dirty-cell');
var isDirty = $(this).hasClass('k-dirty-cell');
if (isDirty == true) {
var cellContent = $(this).context.innerText;
var cellIndex = $(this).context.cellIndex;
// alert(cellContent + cellIndex);
}
});
Could you please help if you can.
Many thanks.
I have a table such as this below:
<table id="Grid">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td class="k-dirty-cell">$100</td>
</tr>
<tr>
<td>February</td>
<td class="k-dirty-cell">$80</td>
</tr>
<tr>
<td>March</td>
<td>$98</td>
</tr>
</tbody>
</table>
I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not.
The class is always in the second column, so I am only interested in iterating through the rows of the second column - i.e., when cellIndex = 2.
I have tried this so far but I'm a bit stuck:
$("#Grid tbody").find('td').each(
function () {
debugger;
// run for specific columns - where validation is needed
// var isDirty = cellToValidate.hasClass('k-dirty-cell');
var isDirty = $(this).hasClass('k-dirty-cell');
if (isDirty == true) {
var cellContent = $(this).context.innerText;
var cellIndex = $(this).context.cellIndex;
// alert(cellContent + cellIndex);
}
});
Could you please help if you can.
Many thanks.
Share Improve this question edited Feb 27, 2014 at 1:20 t_plusplus asked Feb 27, 2014 at 1:09 t_plusplust_plusplus 4,2195 gold badges47 silver badges61 bronze badges3 Answers
Reset to default 3if your primary goal is I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not.
, the following will suffice:
$(function() {
$('td.k-dirty-cell').each(function() {
var cellValue = $(this).text();
//do something with cellValue
if(cellValue) {
//not null
} else {
//null
}
});
});
EDIT:
since there may be a case where the class is on the first column (per ment discussion), the following will select only second column items with the k-dirty-cell
class:
$(function() {
$('tr td:nth-child(2).k-dirty-cell').each(function() {
var cellValue = $(this).text();
alert(cellValue)
//do something with cellValue
if(cellValue) {
//not null
} else {
//null
}
});
});
example fiddle: http://jsfiddle/tztLD/
$(".k-dirty-cell").each(function() {
// your code
});
Should do the trick.
You only need to get all the td
tags whose class is k-dirty-cell
, so this would be enough:
$("#Grid td.k-dirty-cell").each(function() {
if($(this).text().length > 0) {
alert("value: " + $(this).text());
}
}