Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disappearing event listeners #767

Open
dolik-rce opened this issue Oct 19, 2023 · 1 comment
Open

Disappearing event listeners #767

dolik-rce opened this issue Oct 19, 2023 · 1 comment

Comments

@dolik-rce
Copy link

I have noticed, that if I use pre-existing table and pass it to List.js, then any event listeners that were added to the original elements disappear. Little example:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/2.3.1/list.min.js"></script>
  </head>
  <body>
    <table id="users">
      <thead>
        <tr>
          <th class="name sort" data-sort="name">Name</th>
          <th class="born sort" data-sort="born">Year</th>
        </tr>
      </thead>
      <tbody class="list">
        <tr>
          <td class="name"><span>John</span></td>
          <td class="born">1987</td>
        </tr>
        <tr>
          <td class="name"><span>Paul</span></td>
          <td class="born">1985</td>
        </tr>
        <tr>
          <td class="name"><span>Jack</span></td>
          <td class="born">1986</td>
        </tr>
      </tbody>
    </table>
    <script>
      document.querySelectorAll('span').forEach(
          span => span.addEventListener('click', (e) => e.target.style.color='red')
      );
  
      let userList = new List('users', { valueNames: [ 'name', 'born' ] });
    </script>
  </body>
</html>

This is most probably caused by using innerHTML when processing the list. It might be known bug or even intended behavior, but I haven't found any mention of this in documentation. It is unfortunately quite surprising and rather annoying.

If someone comes across this in future and looks for a workaround: Setting the callbacks in onclick attribute instead of using addEventListener('click', ...) seems to work correctly.

@clstevenson
Copy link

clstevenson commented Mar 11, 2024

I can confirm that this happens to me too. Specifically it happens when list.js is applied to a pre-existing table; I didn't have the same issue with pre-existing lists. (Using the onclick attribute did not work in my case.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants