user3633383 user3633383 - 4 months ago 12
jQuery Question

jquery to get id of button inside td of known tr

How do I get the id of a button inside a dataTable table, when the only info I have is which tr contains the td that has the button. There is more than one td and the button may not be the first one. The button id that I require is ".delete_email", note, there may be more than one with the same class name.

I have tried:

var id = $('#view_all_emails1').DataTable().$('tr.selected td:nth-child(5) button[".delete_email"]').attr("id");


But this causes an error:

Uncaught Error: Syntax error, unrecognized expression: tr.selected td:nth-child(5) button[".delete_email"]

The html:

<tr>
<td>Wed 13-07-16</td>
<td><a href="mailto:?????@?????.???.??">????????</a></td>
<td><a href="index.php?a=em&id=5&ebox=1&et=1" onClick="OpenEmail(5); return false;">
<div class="visible-lg hidden-md hidden-sm hidden-xs" style="color:#06F;">The quick brown fox jumps over the lazy dog!</div>
<div class="visible-md hidden-lg hidden-sm hidden-xs" style="color:#06F;">The quick brown fox jumps over the lazy dog!</div>
<div class="visible-sm hidden-lg hidden-md hidden-xs" style="color:#06F;">The quick brown fox jumps over the lazy dog!</div>
<div class="visible-xs hidden-lg hidden-md hidden-sm" style="color:#06F;">The quick brown fox<br />jumps over the lazy<br />dog! </div></a></td>
<td>enquiries</td>
<td><span class="octicon octicon-mail-read" title="Read!"></span></td>
<td>
<input type='hidden' id='this_id' name='this_id' value='5'/><input type='hidden' id='et' name='et' value='1'/><span class="input-group-btn"><button type="button" id="4:5" name="delete_email" class="delete_email btn btn-danger btn-xs" title="Delete this email!"><span class="glyphicon glyphicon-trash"></span></button></span></td>
<td>2.1<small>kb</small></td>
</tr>

Answer

I don't know why you need to go through DataTable API to get this value.

var id = $('#view_all_emails1 tr.selected .delete_email').attr('id');
Comments