0

I have a htmltable that is dynamically created. I have made the rows clickable. I need to pass the row innertext id to the script that fires when the row is clicked.

htmltable:

<table style="width:100%;">
     <tr>
        <th>id</th>
        <th>name</th>
        <th>other info</th>
     </tr>
    <tr>
         <td class='table_row_click'>11</td>
        <td class='table_row_click'>item 2</td>
        <td class='table_row_click'>lmfao</td>
    </tr>
    <tr>
        <td class='table_row_click'>22</td>
        <td class='table_row_click'>item 2</td>
        <td class='table_row_click'>lol</td>
    </tr>
</table>

Click event:

$(document).ready(function ($) {
           $(".table_row_click").click(function (e) {

//I need to use the clicked row ID here for something 

            });
       });
10
  • 2
    Your rows don't have ids? If you're after the innerText of the td, then that's just e.target.innerText Commented Jan 30, 2020 at 20:42
  • Yes i apologise. I require the inner text id, in this example if i clicked the first row i would require the id "11". Commented Jan 30, 2020 at 20:45
  • That looks like it will work thank you. Ill try it now. Commented Jan 30, 2020 at 20:45
  • actually, how will i specify with td has the inner text i want? as i can click any td on that row to fire the event Commented Jan 30, 2020 at 20:46
  • e is the event that was generated by clicking a particular element. e.target is the element the event originated from. Commented Jan 30, 2020 at 20:47

1 Answer 1

1
$(e.target).closest('tr').find('td').first().text();

You can navigate up to the parent tr, find the td elemements, get the first one, and get its text.

Using closest('tr') will work if the class is on the td or tr level, as closest() can match on itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect mate. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.