Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  174] [ 7]  / answers: 1 / hits: 25071  / 11 Years ago, wed, june 26, 2013, 12:00:00

When someone clicks on the Download button in my table, I want to pass the date values from that particular row to a function. Currently I can only pass the date values from the first table row.



This is the selector I'm using:



$(this).parent().find('#period-start');


Which always returns:



[<td id=​period-start>​5/1/2013​</td>]


I've tried combinations of the parent, child, find and closest selectors, but haven't been able to stumble across the correct one to grab the date values from the current row. Thanks.



Table



<table id=tblStatements class=table>
<thead>
<tr>
<th>Period Starting</th>
<th>Period Ending</th>
<th>Download</th>
</tr>
</thead>
<tbody>

<tr>
<td id='period-start'>5/1/2013</td>
<td id='period-end'>5/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>

<tr>
<td id='period-start'>4/1/2013</td>
<td id='period-end'>4/30/2013</td>
<td><button type='submit'>Download</button></td>
</tr>

<tr>
<td id='period-start'>3/1/2013</td>
<td id='period-end'>3/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>

</tbody>
</table>

More From » jquery

 Answers
84

ID's are always supposed to be unique in HTML. So, you might try out this, w/o using an ID:



// Get the first td
var periodStart = $(this).closest('tr').children('td:eq(0)').text();

// Get the second td
var periodEnd = $(this).closest('tr').children('td:eq(1)').text();


FIDDLE DEMO


[#77404] Tuesday, June 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
loganl

Total Points: 424
Total Questions: 86
Total Answers: 112

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;