0

I need to add event listeners to html elements with a data-tooltip attribute.

For elements that are present at page-load, I can do this in a $(document.ready()) block.

Now if I dynamically add such an element to the page at "run-time", I have to manually add those event listeners to the element.

Is there a way I can listen to something like a document-write event (if such a thing exists) and add the event listeners from there ?

PinkTurtle
  • 6,942
  • 3
  • 25
  • 44
  • 2
    There's no such event, but it sounds like your problem can be easily solved by [event delegation](https://javascript.info/event-delegation) (EDIT: which, since you're using jQuery, jQuery has quite a nice API for: https://learn.jquery.com/events/event-delegation/) – Robin Zigmond May 22 '22 at 15:36
  • Sounds like a job for [mutation observer](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe) – vanowm May 22 '22 at 15:47
  • @RobinZigmond got it. Solved with `$(document).on("mouseover", "[data-tooltip]", function(e){...});` Thanks :-) – PinkTurtle May 22 '22 at 15:56
  • @evolutionxbox yes. – PinkTurtle May 22 '22 at 15:57

0 Answers0