The Bootstrap Tooltip seems to be glued in place

Utilizing jQuery, I am dynamically generating a div that includes add and close buttons. Bootstrap tooltips are applied to these buttons for additional functionality. However, a problem arises where the tooltip for the first add button remains visible even when hovering over another add button. See the screenshot for reference. Any suggestions on how to resolve this issue? https://i.stack.imgur.com/J38kd.png

The dynamic divs are created using the jquery clone method.

$(document).on('click', ".addFilesBtn", function(e) {
 e.preventDefault();
 $(".appendClass:first").clone().appendTo".addFiles");
 $('.closeFilesBtn').show();
 $('.closeFilesBtn:first').hide();
});

I've attempted to hide the tooltips using the following code snippet, however, the tooltip for the first button persistently stays visible.

$(document).on('mouseleave','[data-toggle="tooltip"]', function(){
      $(this).tooltip('hide');
});

Updated HTML Code:

<div class="row addFiles">
    <div class="appendClass" style="margin-bottom: 1.5%;">
        <label style="white-space: nowrap;" class="responsive-enter-details col-sm-3 control-label">Select Files</label>
        <div class="col-sm-8 col-md-9">
            <div class="fileinput fileinput-new" data-provides="fileinput">
                <div class="form-control" data-trigger="fileinput">
                    <i class="glyphicon glyphicon-file fileinput-exists"></i> <span class="fileinput-filename">Click to select file</span> <i class="fa fa-upload pull-right"></i>
                </div>
                <input id="inputfile" type="file" style="display: none;">
            </div>
        </div>
        <button class="btn btn-box-tool addFilesBtn" data-toggle="tooltip" title="Click to add more files">
            <i class="fa fa-plus"></i>
        </button>
        <button class="btn btn-box-tool closeFilesBtn" data-toggle="tooltip" title="Click to remove this block">
            <i class="fa fa-times"></i>
        </button>
    </div>
</div>

Link to JS Fiddle: https://jsfiddle.net/gLkrsbxc/4/

Upon inspection in the provided JS Fiddle link, it is evident that the tooltips remain persistent and are not hidden as intended.

Answer №1

Please refer to the latest update for the solution

As stated in the documentation on http://getbootstrap.com/javascript/#tooltips, it is necessary to initialize all tooltips like this:

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

Hence, after adding tooltips to the DOM, make sure to initialize them by adding

$('[data-toggle="tooltip"]').tooltip()
within your click function after cloning.

$(document).on('click', ".addFilesBtn", function(e) {
  e.preventDefault();
  $(".appendClass:first").clone().appendTo(".addFiles");

  //initialize tooltips(add this line)
  $('[data-toggle="tooltip"]').tooltip();

  $('.closeFilesBtn').show();
  $('.closeFilesBtn:first').hide();
});

If initialized correctly, you may not need the hide function.

Update:

I believe that directly calling the initializing function might not work flawlessly when there are DOM manipulation operations taking place. To overcome this, introduce a slight delay with setTimeout after appending and before initialization as shown below:

$(document).on('click', ".addFilesBtn", function(e) {
  e.preventDefault();
  $(".appendClass:first").clone().appendTo(".addFiles");

  //initialize tooltips(give some time for dom changes)
  setTimeout(function() {
    $('[data-toggle="tooltip"]').tooltip();
  }, 50);

  $('.closeFilesBtn').show();
  $('.closeFilesBtn:first').hide();
});

Update 2

Prior to cloning, ensure to hide the tooltip of the button being clicked:

$(document).on('click', ".addFilesBtn", function(e) {
    e.preventDefault();

    //hide the tooltip
    $(this).tooltip('hide');

    $(".appendClass:first").clone().appendTo(".addFiles");
    $('.closeFilesBtn').show();
    $('.closeFilesBtn:first').hide();
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop the continuous AJAX loop or look for an alternative method to retrieve and compare a list of HTML pages

I am in need of a solution to insert a small script onto all of my website pages. The script must include the correct token for each of the 21 different domains I have. However, not all the sites are directly under the 'domain name' but are consi ...

AJAX and Java combination causing issues with logging out in the system

I am working with AJAX using jQuery to trigger a function when clicking a link with the ID logOut. Here is the AJAX code: $("#logOut").click(function(){ $.ajax({ url: "Logout" }); }); This AJAX function calls my Java Servlet shown below: /** ...

Adjust the color of the font within a div element when hovering over it

I've been attempting to modify the text color and add an underline when a user hovers over it. Despite trying various methods, I haven't been successful. I scoured the internet for a solution but couldn't find one that met my specific requi ...

What is the best method for storing a model in a database?

Hello, I am currently attempting to save a model to the database. I am simply inputting the value of a title in order to save it, as my id is set to auto increment. However, I have encountered an issue where my attempts have been unsuccessful. Can someone ...

Perform a JSON POST request from an HTML script to a Node.JS application hosted on a different domain

In an attempt to send string data via a post json request using JavaScript within an .erb.html file, I am facing the challenge of sending it to a node.js app on another domain that uses express to handle incoming requests. After researching online, I have ...

Adjusting the height of a jQuery Mobile collapsible post-expansion

My jQuery collapsible is not resizing properly to fit the expanded content. Below is an example of the collapsible structure: <div class="row collapsibles" data-role="collapsible"> <h1>Supercategory A</h1> <div class="col-xs-7 ...

Swap out the hyperlink text for a dropdown menu when clicked and then revert back

Is there a way to dynamically switch between a label/text and a Kendo Combobox in a div using JavaScript when clicking on the text? The desired functionality includes: Clicking on the text displays the combobox, clicking away from it hides the combobox a ...

Retrieve the Corresponding Content Inside the Div Element

I have a div with text that needs to be matched with comma-separated values: <div class="val"> This is paragraph 1 </div> <div class="val"> This is paragraph 2 </div> Using an Ajax call, I retrieve the ...

Guide on creating an HTML5 rectangle for reuse using the Prototypal Pattern

I'm struggling to grasp Prototypal Inheritance through the use of the Prototypal pattern by creating a rectangle object and an instance of that rectangle. It seems like it should be straightforward, but I'm having trouble understanding why the Re ...

Angular2-starter configuration setup with environment-based variables (such as .env or .conf) for testing and production settings

Frameworks like PHP Laravel often include files for local configuration, separate from dev, test, and production environments. How can a configuration file be provided for an angular-starter project that contains all local environment variable values (su ...

What is the proper way to ensure a pull right display appears correctly?

This is the code I am currently using .tag-container.bottom-border.col-middle h1.text-center {{ tag.name }} button.btn.btn-follow.pull-right(ng-hide="hasFollowed" ng-click="tagArticles.followTag()") Follow I am trying to align the tag name in th ...

What is the best way to utilize a JavaScript variable as a background within an inline style sheet?

I have a fun project for this evening - I am trying to make my website load a different background image every time the page is refreshed. Earlier on in this project, I managed to make the background interact with window size and screen resolution similar ...

Creating a dynamic AJAX function in an MVC framework to validate the ModelState

I am facing a challenge with my Ajax function that triggers automatically upon submitting a contact form. I need to ensure that this function only fires if the form passes model validation. Marriage.cs public class Marriage { [Required] [DisplayN ...

Displaying content on a click event in AngularJS

I am facing an issue with the ng-show directive not working as expected when a user clicks on an icon. The desired behavior is that the parent div should initially display content, but when the play icon is clicked, the contents of the div should become ...

Wait for NodeJS to finish executing the mySQL query

I am attempting to send an object from the controller to the view. To keep my queries separate from the controller, I am loading a JS object (model). My model structure is as follows: function MyDatabase(req) { this._request = req; this._connection = ...

How can you effectively load shared header and panel HTML onto pages located in separate directories using jQuery Mobile?

I am currently developing a multi-page application utilizing jQuery Mobile and incorporating loadPage() to retrieve various pages. The overall structure is outlined below. landing.html /app-pages/page1.html /app-pages/page2.html /app-pages/page3.html ...

Sending the POST request without clicking the Submit button and populating the input field with the temporary information received from

There is a coding issue that I need help with. The goal is to input a card number, retrieve information about the person from an external server API, and then use a button to populate specific input fields (identified by input ID) with the API data without ...

Error with REST service and browser history in ReactJS

I am currently developing my first Java application, which is a REST service built with Spring Boot. It utilizes technologies such as WEB, REST, JPA, Thymeleaf, and MySQL. The API is fully functional, but I wanted to enhance it with a user interface. After ...

CSS code that causes the animated photo banner to reset instead of continuously looping back to the first

I followed a tutorial and created this banner using HTML and CSS. However, I encountered an issue where instead of the banner continuing in loops, it keeps resetting. Since we haven't covered JavaScript yet in university, I'm looking for help to ...

Transfer a csv file from a static webpage to an S3 bucket

Looking to create a webpage for uploading csv files to an S3 bucket? I recently followed a tutorial on the AWS website that might be helpful. Check it out here. I made some modifications to accept filename parameters in my method, and everything seems to ...