最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Bootstrap tooltip for dynamically changing content - Stack Overflow

matteradmin7PV0评论

I use Bootstrap tooltips to create beautiful HTML tooltips for items. I basically have a skill image in the form of a .png image

<img id="skill" src="ice-bolt-inactive.png" data-toggle="tooltip" data-html="true" title="" />

i define the image into a variable

let skillImage = document.getElementById('skill');

then i initiate the tooltip like this:

skillImage.title = '<p class="skill-title">' + iceBolt.name + '</p><p class="skill-text">' + iceBolt.description + '</p><p class="skill-text">' + iceBolt.setLevel() + '</p><p class="skill-text">' + iceBolt.setDamage() + '</p><p class="skill-text">' + iceBolt.setDuration() + '</p><p class="skill-text">' + iceBolt.setManaCost() + '</p><p class="skill-title">' + iceBolt.name + ' Receives Bonuses From: </p><p> ' + iceBolt.description + '</p>';

Now when i click on the skill image, i fire up an onclick function which increments the iceBolt.level++; . This successfully does that ( i also log the iceBolt.level into the console so i confirm it's changing) but the bootstrap tooltip remains unchanged (doesn't reflect the iceBolt.level, even though i've set it up to display it)

Is my usage of the bootstrap tooltip bad or is it that the bootstrap tooltips won't handle something like this. I want to use them as they are responsive and easy to use.

I use Bootstrap tooltips to create beautiful HTML tooltips for items. I basically have a skill image in the form of a .png image

<img id="skill" src="ice-bolt-inactive.png" data-toggle="tooltip" data-html="true" title="" />

i define the image into a variable

let skillImage = document.getElementById('skill');

then i initiate the tooltip like this:

skillImage.title = '<p class="skill-title">' + iceBolt.name + '</p><p class="skill-text">' + iceBolt.description + '</p><p class="skill-text">' + iceBolt.setLevel() + '</p><p class="skill-text">' + iceBolt.setDamage() + '</p><p class="skill-text">' + iceBolt.setDuration() + '</p><p class="skill-text">' + iceBolt.setManaCost() + '</p><p class="skill-title">' + iceBolt.name + ' Receives Bonuses From: </p><p> ' + iceBolt.description + '</p>';

Now when i click on the skill image, i fire up an onclick function which increments the iceBolt.level++; . This successfully does that ( i also log the iceBolt.level into the console so i confirm it's changing) but the bootstrap tooltip remains unchanged (doesn't reflect the iceBolt.level, even though i've set it up to display it)

Is my usage of the bootstrap tooltip bad or is it that the bootstrap tooltips won't handle something like this. I want to use them as they are responsive and easy to use.

Share Improve this question edited Dec 27, 2020 at 20:17 Djongov asked Dec 27, 2020 at 19:53 DjongovDjongov 3253 silver badges14 bronze badges 1
  • IBased on your updated question. I understood what you actually are looking for. I have mimicked your scenario. Use the logic to get hold of your updated data. The idea is the key. You need to take it forward from here :) – Imran Rafiq Rather Commented Dec 28, 2020 at 7:32
Add a ment  | 

3 Answers 3

Reset to default 2

For anyone who is looking in the future. This solution is for Bootstrap - 5, using JQuery and Javascript.

  1. Init the Tooltip

    var tootTipTitleText = 'True or False';
    
    $(document).ready(function () {
    
        //Enable ToolTip
        $('[data-bs-toggle="tooltip"]').tooltip(
            {
                title: tootTipTitleText,
                html: true,
            }
        );
    });
    
  2. Remove the Previous Tooltip on Button Click

    $('[data-bs-toggle="tooltip"]').tooltip("dispose");
    
    1. Get the value to check.

      if(checkBoxValue == true) {
          tootTipTitleText = 'true';
      } else {
          tootTipTitleText = 'false';
      }
      
    2. Init another tooltip

      $('[data-bs-toggle="tooltip"]').tooltip(
          {
              title: tootTipTitleText,
              html: true,
          }
      );
      
    3. Display The Tooltip

      $('[data-bs-toggle="tooltip"]').tooltip("show");
      
  3. Sample HTML and JS Function

function changeToolTipTitle()
{
    $('[data-bs-toggle="tooltip"]').tooltip("dispose");

    var checkBoxValue = $("#toolTipCheckBox").is(":checked");

    if(checkBoxValue == true)
    {
        //title="Checked or Unchecked"
        tootTipTitleText = 'Checked';
    }
    else
    {
        tootTipTitleText = 'Unchecked';
    }

    $('[data-bs-toggle="tooltip"]').tooltip(
        {
            title: tootTipTitleText,
            html: true,
        }
    );

    $('[data-bs-toggle="tooltip"]').tooltip("show");
}
<script src="https://ajax.googleapis./ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div id="toolTipCheckBoxDiv" class="form-check inputCheckBox" data-bs-toggle="tooltip" title="">
<input class="form-check-input" type="checkbox" id="toolTipCheckBox" value="true" onchange="changeToolTipTitle();" />
<label class="form-label" for="toolTipCheckBox">Check</label>
</div>

UPDATE:

Code snipper shared, will help you update your tooltip title whenever some particular event occurs in your application.

The idea is: Setting my title dynamically using a function call which returns the updated data.

 $("#skill").tooltip({
    placement: "right",
    title: userDetails, // userDetail() is a function which will return our updated data
    html: true,
  });

Function will look something like;

// Get user details for tooltip
function userDetails() {
  return tooltipText;
}

The event handler in which we update the content of tooltip.

 document.querySelector("button").addEventListener("click", () => {
  tooltipText += "Add"; // Updating my data
  userDetails();// Sending updated Title
})

FULL CODE:

$(document).ready(function () {
  // Add tooltip
  $("#skill").tooltip({
    placement: "right",
    title: userDetails,
    html: true,
  });
});
var tooltipText = "Hello";
// Get user details for tooltip
function userDetails() {
  return tooltipText;
}

document.querySelector("button").addEventListener("click", () => {
  tooltipText += "Add";
  userDetails();
});
<html lang="en">
  <head>
    <title>Dashboard</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn./bootstrap/4.5.2/css/bootstrap.min.css"
    />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn./font-awesome/4.7.0/css/font-awesome.min.css"
    />
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <img
      id="skill"
      class="skill"
      src="https://picsum.photos/100"
      data-toggle="tooltip"
      data-html="true"
      data-selector="true"
      alt="skill image"
    />
    <p id="ice-bolt-level"></p>
    <button>Click Me</button>

    <script src="https://ajax.googleapis./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare./ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn./bootstrap/4.5.2/js/bootstrap.min.js"></script>

    <script src="script.js"></script>
  </body>
</html>

YOU CAN USE THE SAME LOGIC TO PROCEED FORWARD IN YOU APPLICATION.

This works for Bootstrap 4:

    const title = 'Your new tooltip content';
    $el.attr('data-original-title', title);

(Be sure not to do this instead, as it won't work: $el.data('original-title', title);).

Post a comment

comment list (0)

  1. No comments so far