Skip to content
Advertisement

How can I get the specific user information from the list of users upon click in Javascript?

I am writing a firebase mini chat web app where an admin can be able to privately chat with all the authenticated users.

so I used the firebase cloud-function to fetch the list of all users, code: πŸ‘‡

JavaScript

And from the front-end I called the cloud-function and displayed users using map method. πŸ‘‡

JavaScript

Users are listed successfully. My problem now is if admin click on a specific user, I can be able to get or grab that specific user information like, id, displayName, etc.

thanks as you help me outπŸ™πŸ™πŸ™

Advertisement

Answer

You can bind a click event only on a DOM element. There are some ways to do this even if you added the element with innerHTML. But for simplicity, I’d offer you to not add the elements with innerHTML but with document.createElement which returns the DOM element.

So the logic is:

  1. For each item, create a div element – document.createElement('div')
  2. Set its html similar to what you did – div.innerHTML
  3. Bind it a click event – div.addEventListener('click', ..)

This way, you have the item itself in the addEventListener callback’s scope. That’s why the alert works.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement