-1

HTML:

<table class="datagrid-btable">
   <tbody>
     <tr id="datagrid-row-0">
        <td field="username"></td>
        <td field="firstname"></td>
        <td field="lastname"></td>
        <td field="actions">
            <div>
               <a href="javascript:void(0)" class="editrow" id="1"><span>EDIT</span></a>
               <a href="javascript:void(0)" class="deleterow" id="1"><span>DELETE</span></a>
            </div>
        </td>
     </tr>
     <tr id="datagrid-row-1">
        <td field="username"></td>
        <td field="firstname"></td>
        <td field="lastname"></td>
        <td field="actions">
            <div>
               <a href="javascript:void(0)" class="editrow" id="2"><span>EDIT</span></a>
               <a href="javascript:void(0)" class="editrow" id="2"><span>DELETE</span></a>
            </div>
        </td>
     </tr>
   </tbody>       
</table>

jQuery:

$('.editrow').on('click', function() {
   console.log($(this).attr('id'));
});

Question:

I don't know much of jquery. But I tried I also tried this code: $('.editrow').closest('td') still nothing. Maybe someone who's pro in this can help me.

aldrin27
  • 3,407
  • 3
  • 29
  • 43

2 Answers2

0
  console.log($(this).attr('id');

to:

 console.log($(this).attr('id'));
huan feng
  • 7,307
  • 2
  • 32
  • 56
0

If you put your script at the end of <head>, you should wrap it like this,

$(function(){ 
    $('.editrow').on('click',function(){
    alert($(this).attr('id'));
    });
});

If you don't wrap it with ready function, the script will try to bind the event handlers before the DOM exists.

Otherwise, put your script at the end of the <body>

<script type="text/javascript">
  $('.editrow').on('click',function(){
    alert($(this).attr('id'));
  });
</script>
choz
  • 17,242
  • 4
  • 53
  • 73