CodexBloom - Programming Q&A Platform

jQuery tooltip not displaying properly on dynamically loaded content with Bootstrap 5

πŸ‘€ Views: 29 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-18
jquery bootstrap ajax JavaScript

I've tried everything I can think of but I'm having trouble getting jQuery tooltips to display correctly on elements that are dynamically loaded into the DOM using AJAX in a Bootstrap 5 environment. I initialize the tooltips after the content is loaded, but they seem to be misplaced or do not show at all. Here’s a simplified version of my code: ```javascript $(document).ready(function() { // Load content via AJAX $('#loadContent').on('click', function() { $.ajax({ url: 'content.html', method: 'GET', success: function(data) { $('#dynamicContainer').html(data); // Initialize tooltips after loading content $('[data-toggle="tooltip"]').tooltip(); }, behavior: function() { console.behavior('Failed to load content.'); } }); }); }); ``` In the `content.html`, I have elements like this: ```html <button type="button" data-toggle="tooltip" title="Tooltip on button">Hover me</button> ``` When I click the button to load the content, I can see the button appear, but hovering over it doesn't show the tooltip. I've also checked to make sure that Bootstrap and jQuery are correctly included in my project. Here are the versions I’m using: - jQuery: 3.6.0 - Bootstrap: 5.1.3 I tried calling `tooltip()` both before and after setting the HTML, but the tooltips still do not appear or are displayed incorrectly. Is there something I’m missing, or a specific initialization step for Bootstrap tooltips that I should be aware of when using jQuery with dynamically created elements? Any insights would be greatly appreciated! For context: I'm using Javascript on Debian. What would be the recommended way to handle this?