0

So, I have a table with update button, and I want to be able to fetch a column from the row button was clicked on.

Below is the html code for table

<table class="table table-hover" style="width: 99%">
        <thead class="thead-dark" align="center">
            <tr>
                <th>ID</th>
                <th>Trainee Class</th>
                <th>Start Date</th>
                <th>Edit</th>
                <th>Trainees</th>
            </tr>
        </thead>
        <tbody align="center">
            <tr>
                <td class="nr">J001</td>
                <td>Java Stream</td>
                <td>01-April-2018</td>
                <td><button onclick="updateData()">Update</button>
                    <button>Remove</button></td>
                <td><button>View</button></td>
            </tr>
 <table>

So, when I click update, i want to fetch "J001" (first column of the update button row).

I tried below, but it's not fetching.

function updateData(){
     var $item = $(this).closest("tr")   
                 .find(".nr")    
                 .text();        

     alert($item);      
}

Any help or suggestion will be appreciated.

1 Answer 1

2

You have to pass this in your update function call,

Your Html should be,

<table class="table table-hover" style="width: 99%">
        <thead class="thead-dark" align="center">
            <tr>
                <th>ID</th>
                <th>Trainee Class</th>
                <th>Start Date</th>
                <th>Edit</th>
                <th>Trainees</th>
            </tr>
        </thead>
        <tbody align="center">
            <tr>
                <td class="nr">J001</td>
                <td>Java Stream</td>
                <td>01-April-2018</td>
                <td><button onclick="updateData(this)">Update</button>
                    <button>Remove</button></td>
                <td><button>View</button></td>
            </tr>
 <table>

Jquery function should be,

function updateData(e){
     var $item = $(e).closest("tr")   
                 .find("td:first")    
                 .text(); 
     alert($item);      
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. Thanks @JeswinRebil

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.