ReeseB
ReeseB

Reputation: 297

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>

Upvotes: 2

Views: 204

Answers (3)

Ranjeet Singh
Ranjeet Singh

Reputation: 924

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>

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

Your TD does not have a link_id attribute, your <a> has it. I've created an example showing both versions:

$(document).ready(function(){
        $('#mashed_row a').click(function () {
            console.log("TD:", $(this).closest('tr').find('td').attr('link_id'));
            console.log("A:", $(this).closest('tr').find('a').attr('link_id'));
            // or if you really want to get the attribute from the clicked item, it's even easier:
            console.log("direct:", $(this).attr('link_id'));
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
  <tr id="mashed_row">
    <td class="linked-title dark unpadded" link_id="link_id_TD">
      <a href="#" id="linksplode" link_id="link_id_A">Hello</a>
    </td>
  </tr>
</table>

Upvotes: 1

Dhara Parmar
Dhara Parmar

Reputation: 8101

Simply use $(this).attr('link_id')

$(document).ready(function(){
    $('#mashed_row a').click(function () {
        alert($(this).attr('link_id'));
    });
});

Upvotes: 1

Related Questions