1

I want to get the first column row value on each row href click EDIT . for eg if i click on first row href then i should get alert with its first column row value .

$(document).ready(function () {
        var jsonData = [{
            id: 'A01',
            name: 'Fadhly'
        }, {
            id: 'A02',
            name: 'Permata'
        }, {
            id: 'A03',
            name: 'Haura'
        }, {
            id: 'A04',
            name: 'Aira'
        }];

        $.each(jsonData, function (key, val) {
            var tr = '<tr>';
            tr += '<td>' + (key + 1) + '</td>';
            tr += '<td>' + val.id + '</td>';
            tr += '<td>' + val.name + '</td>';
            tr += '<td><a href="#menu1">Edit</a</td>';
            tr += '</tr>';
            $('tbody').append(tr);
        });

My HTML Code

<script           src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border="1" width="100%">
<thead>
<tr>
  <th>#</th>
  <th>Id</th>
  <th>Name</th>
  <th>Action</th>
</tr>
</thead>

<tbody>
</tbody>
</table>
M J
  • 31
  • 8
  • In above table if i click on first row edit href then it should print id in alert box. i dont want on button click only href click . – M J Feb 17 '17 at 15:51
  • That's not [JSON](http://www.json.org/) -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Feb 17 '17 at 15:51
  • PLease check my updated code and please help to get value on each row href click . – M J Feb 17 '17 at 15:57

3 Answers3

1

Given a generated HTML structure as follows with n rows being dynamically added to <tbody>:

<table border="1" width="100%">
  <thead>
    <tr>
      <th>#</th>
      <th>Id</th>
      <th>Name</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>
        <a>Edit</a>
      </td>
    </tr>
    <tr><!-- n dynamically generated rows --></tr>
  </tbody>
</table>

You could use the following jQuery to execute alert() of the text within the first column of the respective row using click handler with a context provided:

$(document).ready(function(){
    $('tbody').on('click', 'a', function(){
        var value = $(this).closest('tr').find('td').first().text();
        alert(value);
    });
});

Here is a jsfiddle demonstrating the functionality. It demonstrates the value of the first column being alerted as well as shows dynamic/added rows via click event executing the event on <a> click. This is because the <a> elements aren't directly targeted, they are supplied as context to permanent DOM element such as which won't be added or removed.

Update

If you need to grab other columns you could structure the jQuery query using eq() to grab a zero (0) based index. For example if you need the second column you could:

// first column
var value = $(this).closest('tr').find('td').eq(0).text();

// second column
var value = $(this).closest('tr').find('td').eq(1).text();

// third column
var value = $(this).closest('tr').find('td').eq(2).text();

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
0
    $.each(jsonData, function (key, val) {
        var tr = '<tr>';
        tr += '<td>' + (key + 1) + '</td>';
        tr += '<td>' + val.id + '</td>';
        tr += '<td>' + val.name + '</td>';
        tr += '<td><button class="delete" data-key="' + (key + 1) + '" onclick="alert('+(key+1)+')"><a href="#menu1">Edit</a></button></td>';
        tr += '</tr>';
        $('tbody').append(tr);
    });

Simply add it to the onclick attribute...

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

check this out.

this works perfectly

$(document).ready(function () {
        var jsonData = [{
            id: 'A01',
            name: 'Fadhly'
        }, {
            id: 'A02',
            name: 'Permata'
        }, {
            id: 'A03',
            name: 'Haura'
        }, {
            id: 'A04',
            name: 'Aira'
        }];

        $.each(jsonData, function (key, val) {
            var tr = '<tr>';
            tr += '<td>' + (key + 1) + '</td>';
            tr += '<td>' + val.id + '</td>';
            tr += '<td>' + val.name + '</td>';
            tr += '<td><button class="delete" data-key="' + (key + 1) + '"><a href="#menu1">Edit</a></button></td>';
            tr += '</tr>';
            $('tbody').append(tr);
        });

         $('tbody .delete a').click(function(){
             alert($(this).closest('tr').find('td:first-child').text());
         });
});

here is the updated jsfiddle link. check it out. https://jsfiddle.net/86n7qkpe/

Inus Saha
  • 1,918
  • 11
  • 17
  • added jsfiddle link, check that out. – Inus Saha Feb 17 '17 at 16:11
  • I removed first column from hmtl table and then i should get id but still it is giving me 1 as output . but it should give me the value of first column row – M J Feb 17 '17 at 16:16
  • Thnx for the explaination and for answer also . Both you and Alexander gave right answer i m confused to whose answer i should accept and tick right – M J Feb 17 '17 at 16:46
  • I understand that, but thank you very much for accepting mine :) – Inus Saha Feb 17 '17 at 17:01
  • How can i add pagination to above html table . – M J Feb 23 '17 at 06:17