1

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>
Drunken M
  • 2,013
  • 2
  • 11
  • 18

2 Answers2

1

From this- Event binding on dynamically created elements?

on function in jQuery can attach event to already created elements only directly.

For example, you can use following code for dynamically created table2-

$('body').on('click', '.table2 a.name', function (event){
//Code here
});

When there is a click on the body, check if target is .table2 a.name, if yes, execute the function.

  • With `$(this)` selector I can execute the dinamically created table 2 properly. No problem there. The issue here is it can't executed both tables at once. I think there's still incorrect syntax with this combined selector `$('.table1 a.name, .table2 a.name')` cause it only can execute one table at a time. – Drunken M Sep 12 '18 at 09:37
  • @DrunkenM, Use `$(document).on('click', '.table1 a.name, .table2 a.name', function(){ //Your code})` – Satpal Sep 12 '18 at 12:57
  • @Satpal Still not working. It is not executing both tables. http://jsfiddle.net/2rt2t/342/ – Drunken M Sep 12 '18 at 13:23
  • @Satpal I found a workaround using a `filter function`. Do you still want to answer this question? Please remove the duplicate as the reference link doesn't show the right solution to this question. – Drunken M Sep 12 '18 at 15:47
0

I found a workaround for this post.

The problem is in jQuery the function this on click function only refer to the element that got clicked. It's never counted on other elements which is declared as selectors even if they have identical contents.

So the solution if you have same content in multiple elements is to use filter function to search for same values and then you can threated them all as one in your action.

In my case I just need to marked the same a href values in all elements.

The updated snippet

$('.table1 a.name, .table2 a.name').click(function(c){
 c.preventDefault();
 var $item = $(this).attr('href'); 
 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);
    
    var marked =  $('.table1 a.name, .table2 a.name').filter(function() {
       $('img.checked').not(marked).remove();
        return $(this).attr('href') ===  $item;
    }).before(checked);   
    
    
  //});
});
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>

<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>
Drunken M
  • 2,013
  • 2
  • 11
  • 18