I wrote a HTML file with a table. This table has about 7 entries (rows with data) by default. When the user clicks on one of these row the background color changes and it becomes marked, I did it with the jquery toggle function. I also have a button which adds further rows in this table, also with jquery. My problem: When I (user) add some rows with that button and click on those new added rows the background color doesn't change but the background color of default rows change.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".row").click(function(){
$(this).toggleClass("activeRow");
});
$(document).on('click', '#addButton', function(){
$(".myTable tbody").append(" <tr class="row"> <td>New</td> <td>This</td> <td>is</td> <td>great</td> </tr>");
});
});
</script>
<style>
.activeRow{
background-color:#B0BED9;
}
.myTable {
border-collapse: collapse;
width: 80%;
clear:both;
margin-top: 0;
border-spacing: 0;
table-layout: fixed;
border-bottom: 1px solid #000000;
}
.myTable tbody, .myTable thead{
display: block;
width: 100%;
}
.myTable tbody{
overflow: auto;
height: 300px;
}
.myTable th, td{
width: 450px;
}
</style
<table class="myTable">
<thead>
<tr id="headRow">
<th>alo</th>
<th>salam</th>
<th>kissa</th>
<th>chissa</th>
</tr>
</thead>
<tbody class="bodyRows">
<tr class="row" >
<td>Microsoft</td>
<td>Saber</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Huawei</td>
<td>20.3</td>
<td>30.5</td>
<td>23.5</td>
</tr>
<tr class="row">
<td>Google</td>
<td>50.2</td>
<td>40.63</td>
<td>45.23</td>
</tr>
<tr class="row">
<td>Apple</td>
<td>25.4</td>
<td>30.2</td>
<td>33.3</td>
</tr>
<tr class="row">
<td>IBM</td>
<td>20.4</td>
<td>15.6</td>
<td>22.3</td>
</tr>
</tbody>
</table>
<button id="addButton" class="tableButton">Add Row</button>