ReeseB ReeseB - 5 months ago 14
Javascript Question

Need to get table row's link attribute in JQuery and

I have a table which I has links on each row. I am trying to use AJAX to retrieve an array of data from the database via PHP and return the data which will print in another DIV.

I am trying to get the attribute value (link ID) that I assign to each row and pass this value to the query. I am not able to get the link ID, it does not display when I test issuing an alert. I read several post and tried a few things but no good. The alert popup displays "undefined".

<tr id="mashed_row">
<td class="linked-title dark unpadded">
<a href="#" id="linksplode" link_id="<?php echo $link['linkid']; ?>"><?php echo $link['keywords']; ?></a>
</td></tr>

<script type="text/javascript">
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).closest('tr').find('td').attr('link_id'));
});
});
</script>

Answer

You are trying to get td attr which is not there, try this, hope it helps :-

 $(document).ready(function(){
        $('#mashed_row a').click(function () {
            alert($(this).attr('link_id'));
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
  <tr id="mashed_row">
    <td class="linked-title dark unpadded">
      <a href="javascript:;" id="linksplode" link_id="12">key words</a>
    </td>
  </tr>
<table>

Comments