Reputation: 928
I have page that I'm working on that contains a table with a header row, several rows of text inputs, and a total row. When a user enters numbers in the inputs, the sum of each column is calculated in the totals row. This calculation is run when an input's change()
event is fired.
When the page is loaded, I'd like to be able to trigger a change()
event on the text inputs in the first row of the table. I'm having some trouble figuring out the correct selector to use.
Exampe of table structure:
<table>
<tr class="tblRow headerRow">
<!-- header cells -->
</tr>
<tr class="altTblRow">
<td class="tableCell">
<input type="text" id="input1-1">
</td>
<td class="tableCell">
<input type="text" id="input1-2">
</td>
<td class="tableCell">
<input type="text" id="input1-3">
</td>
</tr>
<tr class="tblRow">
<td class="tableCell">
<input type="text" id="input2-1">
</td>
<td class="tableCell">
<input type="text" id="input2-2">
</td>
<td class="tableCell">
<input type="text" id="input2-3">
</td>
</tr>
<tr class="tblRow">
<td class="tableCell">
<input type="text" id="input3-1">
</td>
<td class="tableCell">
<input type="text" id="input3-2">
</td>
<td class="tableCell">
<input type="text" id="input3-3">
</td>
</tr>
<tr class="altTblRow totalRow">
<!-- total cells -->
</tr>
</table>
I tried using a couple of selectors, but haven't been able to get what I'm looking for.
$('.tblRow:first input').change();
$('.tblRow input').first().change();
$('.tblRow input').parent('tr').find('input').change();
Is there a selector that I can use for just the inputs in the first table row, or do I have to get the first input
and get the row using .parent()
?
(Note: ID's have been changed in the code sample; otherwise I'd use 'input[class*=input1]'
)
Upvotes: 2
Views: 8737
Reputation: 253496
I'd suggest, though untested:
$('tr:not(".headerRow")').first().find('input').change();
Further, though, I'd suggest reworking your HTML to take advantage of semantics (Which will simplify your jQuery):
<table>
<thead>
<tr class="tblRow headerRow">
<!-- header cells -->
</tr>
</thead>
<tfoot>
<tr class="altTblRow totalRow">
<!-- total cells -->
</tr>
</tfoot>
<tbody>
<tr class="altTblRow">
<td class="tableCell">
<input type="text" id="input1-1">
</td>
<td class="tableCell">
<input type="text" id="input1-2">
</td>
<td class="tableCell">
<input type="text" id="input1-3">
</td>
</tr>
<!-- other rows -->
</tbody>
</table>
Which would allow you to simply use:
$('tbody tr').first().find('input').change();
References:
Upvotes: 2
Reputation: 1605
Insetead of using table row try While(parameter)and try it and than see what result you are getting if you get clear tell me if not show me the exact error what you are getting
Upvotes: 2