I have 2 tables with different class that have same contain. Table 1 shown in main section and the second table shown on a popup function. Both table still on the same page html.
I have a click function using 2 classes selectors which is I hope can fires both tables on single click and change both tables contain. But the click function only works in one table, not both tables.
Note that the second table is dinamically created and not always existed.
How to make it fires both tables on each click and not return error if second table doesn't exist.
My code:
$('.table1 a.name, .table2 a.name').click(function(c){
c.preventDefault();
var $item = $(this);
var checked = $('<img class="checked" src="https://findicons.com/files/icons/1964/colorcons_green/128/checkmark.png" width="30" height="30">');
//$.post("", {data:datas},function(data){
// some ajax result
$($item).before(checked);
$('img.checked').not(checked).remove();
//});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Table 1</h2>
<table class="table1">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com">Alfreds Futterkiste</a></td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com/2">Centro comercial Moctezuma</a></td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com/3">Ernst Handel</a></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
</table>
<br>
<h2>Table 2</h2>
<table class="table2">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com">Alfreds Futterkiste</a></td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com/2">Centro comercial Moctezuma</a></td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td><a class="name" href="https://stackoverflow.com/3">Ernst Handel</a></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
</table>