-1

I'm trying to get the first value of the row in my html table when I click in each delete button...

The table is populated from SQL database...

the image below!

I will put only the necessary code:

enter image description here

HTML Button:

<button type="button" class="btn btn-danger btn_delete" id="btn_delete" onclick="delete_function()">Delete</button>

Javascript Code:

<script type="text/javascript">
    function delete_function() {
        var id = $('#clients tbody').find('td:first').text();
        alert(id);      
    }
</script>

If I click in any button I always receive the alert with same result:

114

How I can get the same id number value that exist in the same line of the button clicked??? I accept Javascript and Jquery advices

abelvf
  • 49
  • 3
  • 9
  • 1
    You can't use the same ID on each button. – Leeish Jun 16 '15 at 20:07
  • Additionally, if you are outputing this info from a server side language, just include the id on the button with `data-id=114` and then you can easily get it with `$('.btn_delete').data('id')` – Leeish Jun 16 '15 at 20:11

4 Answers4

2

Change your code to :

<button type="button" class="btn btn-danger btn_delete" id="btn_delete" onclick="delete_function(this)">Delete</button>

Use first-child

<script type="text/javascript">
    function delete_function(obj) {
        var id = $(obj).closest('tr').find('td:first-child').text();
        alert(id);      
    }
</script>

One thing to mention : you should not use onclick in the html :

$(".btn_delete").on('click',function (){
  var id = $(this).closest('tr').find('td:first-child').text();
  alert(id); 

});
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1
$('.btn_delete').click(function(){
    var id = $(this).parents('tr').find('td:first-child').text();
    alert(id);
}

onClick is always a bad idea.

jQuery.click() vs onClick

Why is using onClick() in HTML a bad practice?

Community
  • 1
  • 1
Leeish
  • 5,203
  • 2
  • 17
  • 45
  • 1
    I suggest you to use `closest` rather than `parents` – Royi Namir Jun 16 '15 at 20:13
  • Why? Is there a problem? It skips the self-check. – Leeish Jun 16 '15 at 20:14
  • YES . there is a problem : `parents` :Travels up the DOM tree to the document’s root element, adding each ancestor element to a temporary collection; it **then** filters that collection based on a selector if one is supplied. closest doesnt do it. it travels up the DOM tree until it finds a match for the supplied selector – Royi Namir Jun 16 '15 at 20:15
  • Ah I didn't know that and was just reading up on it to see if it was headed towards depreciation or something. – Leeish Jun 16 '15 at 20:16
  • No need for deprecations. they do the _same_ thing. but the way they do it is different. – Royi Namir Jun 16 '15 at 20:17
  • Right, I was checking because you said to use closest. I thought it might be depreciated. I thought it headed up the DOM to the first one it finds, apparently like closest does. I didn't know that. I thought closest looked downward and not just upward too. – Leeish Jun 16 '15 at 20:18
0

There's many ways you can go about doing this, but one way would be to set the id directly on the button itself so you don't have to traverse the DOM to get it.

<button class="delete" data-id="114">Delete</button>

Then, in your JavaScript

$('button.delete').on('click', function( e ) {
    console.log($(this).data('id'));
});
chazsolo
  • 7,873
  • 1
  • 20
  • 44
0

You always get the same result because you are always getting the same element. To get the value related to the button that you pressed, you need to involve the button when you locate the element.

As you are using jQuery, you can use it to bind the events also. Remove the onclick attribute from the HTML

<button type="button" class="btn btn-danger btn_delete" id="btn_delete">Delete</button>

In the event handler you use the button that was clicked (this) to locate the first cell in the same row:

$(document).ready(function(){

  $('#clients .btn_delete').click(function() {
    var id = $(this).closest('tr').find('td:first').text();
    alert(id);      
  }

});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005