0

I want to call a Javascript function from a table row cell. I need to pass the id of that row as well.

In one cell I use an href (which does popup my edit dialog), but does not pass the Id (BrId). The next one, well ideally a button which invokes a Javascript function (though I've seen code/functions which associates a click event function within $(document).ready(function() {.....etc}) but unsure if this will pick up the required Id (BrId) which is a primary key to a database table.

Code is:

foreach ($myrows as $row) {               
       echo "<tr>"; 

         echo '<td style="border:none;">' .$row->BrId. '</td>'; 
         echo '......'
         echo '......'
         echo '<td style="border:none;"><a href="#dialog" name="modal">Edit this branch </td>';
         echo '<td style="border:none;"><button onclick="EditBranch (1)"></td>';
 }

Ideally the function would also show my popup div ( id= dialog ) as the "a href="#dialog" name="modal" does.

If this helps, here's a section of the script:

$(document).ready(function() {

     //select all the a tags with name equal to modal
     $('a[name=modal]').click(function(e) {

     //Cancel the link behavior
     e.preventDefault();

     //Get the A tag 
     var id = $(this).attr('href');  //gets me my div id

    //other code for transition effects and positioning of my div....
}

1 Answer 1

2

You can pass the row id through a custom html data attribute :

echo '<a href="#dialog" name="modal" data-id="' . $row->BrId . '"> Edit this branch </a>';

Then, you simply retrieve it like that :

var id = $(this).attr('data-id');
Sign up to request clarification or add additional context in comments.

Comments

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.