0

I have a PHP table generated using a MySQL database, I want to run a jquery function by clicking on a button placed in a field in the table. Since the button id is dynamic, Is there a way to trigger the button id and run the jQuery?

PHP Code :

echo "<td>
<button id='".$row['S_No']."'value='".$row['S_No'].">".$row['S_No']."</button></td>";

JQuery Code:

  $(document).ready(function(){
    $("#buttonId").click(function(){
      // Code goes here
  });
});
1
  • Don't use an ID, use a class. <button class="js-myfunc" ... other php stuff... /> then $('.js-myfunc').on('click', // code );. Commented Jun 10, 2018 at 19:48

1 Answer 1

1

Use class instead id

echo "<td>
<button class="btn-class" id='".$row['S_No']."'value='".$row['S_No'].">".$row['S_No']."</button></td>";

$(document).ready(function(){
    $(".btn-class").click(function(){
      // Code goes here
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Worked as a charm! Thank you! much appreciated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.