Thousand
Thousand

Reputation: 6638

Get value of button inside TD datatables

See the following HTML: enter image description here

i need to get the value (417762) of this button. I"m trying to do it like this but it doesn't work (see the else clause beneath). I get "undefined" all the time.

$('#migrationssearchtable tbody').on('click',
            'td.details-control',
            function() {
                var tr = $(this).closest('tr');
                var row = table.row(tr);

                if (row.child.isShown()) {
                    row.child.hide();
                    tr.removeClass('shown');
                }
                else {

                    var v = tr.find(".details-control"); // this finds the HTML of the TD
                    var o = v.find(".btn btn-requeue"); // this does not work, gives undefined.  


                }
            }
        );

Upvotes: 0

Views: 555

Answers (2)

Faraz PV
Faraz PV

Reputation: 522

Replace

var o = v.find(".btn btn-requeue");

With

var o = v.find(".btn.btn-requeue");

You can check the fiddle

http://jsfiddle.net/rgs5a1xy/5/

Upvotes: 2

JasperZelf
JasperZelf

Reputation: 2844

You're looking for v.find(".btn btn-requeue");. Notice that btn-requeue has not got a period in front of it. That means it jQuery will be looking for an element with a btn-requeuetag inside of the element with the btnclass.

The correct way would be .btn.btn-requeue, or just .btn-requeue.

I've changed it to v.find(".btn.btn-requeue"); in the code below, and that seems to do the trick.

$(function(){

	$('#migrationssearchtable tbody').on('click', 'td.details-control',function() {
		var tr = $(this).closest('tr');
  	var v = tr.find(".details-control"); // this finds the HTML of the TD

  	var o = v.find(".btn.btn-requeue"); 
   alert('the value is: '+o.val());
  });
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="migrationssearchtable">
<tbody>
<tr>
  <td class="details-control">
    <button class="btn btn-requeue" value="417762">
    ++
    </button>
  </td>
</tr>
</tbody>
</table>

Upvotes: 1

Related Questions