2

I get a Table with Data from a MySql Database via Ajax when i click a Button. It is displayed into a Div. I get the whole HTML with content from my .php script on the Server.

...
 $statement->execute();

    echo "   <table id='tableID' onClick='idfromclick(event)' class='table table-bordered table-hover'>
    <thead>
      <tr>
        <th>Name</th>
        <th>Datum</th>
        <th>ID</th>
        <th>Position</th>
        <th>Notiz</th>
      </tr>
    </thead>
    <tbody>  ";

foreach ($statement -> fetchAll() as $result)
{   
    echo "<tr>";
    echo "<td align=center>$result[0]</td>";
    echo "<td align=center>$result[1]</td>";
    echo "<td align=center>$result[2]</td>";
    echo "<td align=center>$result[3]</td>";
    echo "<td align=center>$result[4]</td>";
    echo "</tr>";
}
echo "</tbody>
  </table>";
...

When I click on a Row I only need the ID which I get with

function idfromclick(e){    
      localStorage.setItem("ClickID",e.target.parentNode.cells[2].innerHTML);
    }

Now i want that the selected (clicked) Row is in a different Color until another Row is clicked.

This one works with a local Table

 <table id="blub" class="table table-bordered table-hover">
    <tbody>
        <tr><td>Something..blah blah</td><td>Something...blah blah</td><td>Something...blah blah</td></tr>
        <tr><td>Something ...blah blah</td></tr>
        <tr><td>Something ...blah blah</td></tr>  
        <tr><td>Something ..blah blah</td></tr>
    </tbody>
</table>

      <script> $('table tr').each(function(a,b){
    $(b).click(function(){
         $('table tr').css('background','#ffffff');
         $(this).css('background','#c5e9b8');   
    });
});
      </script>

BUT it dont work on my other (php) Table. It seems like no jQuery works?

Why is that so and can i make jQuery works?

The Solutions with only JS are much longer and complicated.

2
  • The Solutions with only JS are much longer and complicated. this is why we have JQuery Commented May 12, 2017 at 18:16
  • It won't work mostly because when you call your table, you already defined all the click events for your rows in your page, which don't include your .php table Commented May 12, 2017 at 18:18

2 Answers 2

2

Try binding the event to the table and not for the each row:

$('body').on("click", "table tr", function() {
    $(this).siblings().css('background','#ffffff');
    $(this).css('background','#c5e9b8');   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="blub" class="table table-bordered table-hover">
    <tbody>
        <tr><td>Something..blah blah</td>
        <td>Something...blah blah</td><td>Something...blah blah</td></tr>
        <tr><td>Something ...blah blah</td></tr>
        <tr><td>Something ...blah blah</td></tr>  
        <tr><td>Something ..blah blah</td></tr>
    </tbody>
</table>

This will work for any table you have in your page. What probably happened in your code is that you were binding the events to an already created table, so that new table won't have the events.

Sign up to request clarification or add additional context in comments.

Comments

0

This is because jQuery binds element events on load. To make it work you need to re-bind events after your AJAX call completed.

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.