Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling jQuery click events inside a table

I have a table that contains several rows each containing one button and one textbox.

What is the simplest way to trigger alert with a textbox value?

like image 973
mko Avatar asked Dec 06 '25 08:12

mko


1 Answers

You can bind a click handler to the button which traverses up the DOM to its containing <tr>, and then finds the textbox from there:

// Bind a click handler to all buttons in the table...
$('table :button').click(function () {

  var text = $(this)    // "this" is the button which was clicked
    .closest('tr')      // find the <tr> which contains it...
    .find(':text')      // find the text box within that <tr>...
    .val();             // and get its value

  alert(text);
});
like image 65
meagar Avatar answered Dec 08 '25 21:12

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!