Akil M Belim
Akil M Belim

Reputation: 125

how to fire event of button in table

<table style="width: 5%;" class="pagination" id="paging">
    <tfoot>
        <td>
            <span>1</span>
        </td>
        <td>
            <input type="button" name="edit" value="2"> 
        </td>   
    </tfoot>
</table>

I want to fire click event of button using jQuery and I also want to get value of it.

Upvotes: 0

Views: 354

Answers (4)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

This should work however I would suggest a class on the button to better identify it:

$('table').on('click','input[type=button]',function(){
    alert($(this).val());
});

Note that I also isolate to the "button" type here in case you have other inputs in the table somewhere.

Note: IF you only want the value outside the click handler you can use:

var buttonValue = $('table').find('input[type=button]').val();

Using the class on the table selector:

$('table.pagination').on('click','input[type=button]',function(){
    alert($(this).val());
});

Even MORE specific, use the table ID

$('#paging').on('click','input[type=button]',function(){
    alert($(this).val());
});

Upvotes: 1

Tormod Haugene
Tormod Haugene

Reputation: 3686

Another option almost identical to the others, but a bit more complete:

Change

<input type="button" name="edit" value="2"> 

to

<input type="button" name="edit" value="2" id="MyButtonID">

Then attach a function to it like mentioned by others:

$(function() {
   $("#MyButtonID").click( function()
      {
          // Do something, like pop a message
          alert("It works!");
      }
   );
});

Upvotes: 0

Odward
Odward

Reputation: 113

$(function() {
  $("#button").click( function()
       {
         alert('button '+$(this).attr('id')+' with text '+$(this).text()+' clicked');
       }
  );
});

Upvotes: 0

AloneInTheDark
AloneInTheDark

Reputation: 938

I assume you want to get button's value.

$("#edit").click(function(){
var editvalue=$("#edit").val();

    alert(editvalue); //shows value on a messagebox

});

here is the fiddle.

Upvotes: 0

Related Questions