Event listeners not attaching after an operation on the page
Hello, for some reason when I use the search field or add a cyclist, the element gets added to the page but the show details and delete button doesnt work. They work on reload however. It's an even listener issue but I tried all sorts of things but doesnt seem to be working.
This are the buttons:
24 Replies
this is all the code concerning this issue
If attachEventListeners() is the only code that adds event listeners, and it only gets called upon loading of the page, that won't include any new items added later. So the problem is that you are not also adding event listeners immediately after new elements are created.
Might be a good case for a JavaScript class
This is not needed bc I have this class in my Spring application. Js is used for adding, updating, searching and deleting records without page refresh.
So what you are saying is that when i search something, I should also attach the listeners?
And not just on a page load?
Why not have the event listeners in the class if they all have the same buttons ?
Im not sure if its even possible to have listeners in Java spring application
Ok
Idk what a spring application is
then just add event listeners scoped to the buttons you need
What I am saying is the event listeners must be attached to all new elements in the DOM that you expect to respond to user interaction. This must be done regardless of the reason that the new elements are being added. It doesn't matter if it's because of search or some other reason.
is a framework for the Java platform
You could also use event delegation. That would involve placing an event listener only on the container and checking the event target to determine which element the event happened on.
I will try it now. Can I get back to you if it doesnt work?
It will work, event delegation is what you need. Thatβs the proper word for what I said about an event listener scoped to the buttons. Add it on the container, like say the ul that holds each li row . Then:
Etc
Also from a quick google you can add event listeners on a class in Java Spring application but looks like a lot of code to create and publish @EventListener
I reattached the listener when for example I search and table is loaded with new data but I just get this error message
This is what I did
What's weird is that delete event listener works
but not for show details