skip to Main Content

I’ve already looked over several posts on stack overflow asking virtually the exact same question yet none of what I found on those questions has helped. I’m very new to JQuery and Bootstrap so maybe I’m just missing some really simple thing.

I want to be able to to change the title of the tooltip on different elements after the first initialization(ideally multiple times after initialization.) A simplified version of what I’m dealing with:

<canvas id="bag0"  data-toggle="tooltip" title="test">
</canvas>
...
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

$(document).ready(function(){
            $('#bag0').data('tooltip',false)          
                  .tooltip({ title: 'new text'});
            $('[data-toggle="tooltip"]').tooltip();

});

This method to change the title was given from posting: How to overwrite twitter bootstrap tooltip?

The tooltip always reads “test.” I’ve tinkered with a few others things to no avail. I suspect I’m overlooking something obvious.

8

Answers


  1. This might help

    $('[data-toggle="tooltip"]').attr("title","NEW TEXT");
    

    If you want to for a particular element, say a <div>

    $('div[data-toggle="tooltip"]').attr("title","NEW TEXT");
    
    Login or Signup to reply.
  2. $(element).attr('title', 'NEW_TITLE').tooltip('fixTitle').tooltip('show');

    Above code might help you.

    $.tooltip(string) calls any function within the Tooltip class. And if you look at Tooltip.fixTitle, it fetches the data-original-title attribute and replaces the title value with it.

    You can use the element id or class to make it more specific.

    • Help 🙂
    Login or Signup to reply.
  3. Try this

    $(element).tooltip().attr('data-original-title', "new title");
    

    Source: github bootstrap issue

    Login or Signup to reply.
  4. Bootstrap 4

    $('#topic_1').tooltip('dispose').tooltip({title: 'Goodbye'}).tooltip('show')
    
    $('#topic_1').tooltip({title: 'Hello'}).tooltip('show');
    setTimeout( function() {
      $('#topic_1').tooltip('dispose').tooltip({title: 'Goodbye'}).tooltip('show');
    }, 5000);
    #topic_1 {
      border: 1px solid red;
      margin: 50px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
    
    <div id="topic_1">Topic 1</div>
    Login or Signup to reply.
  5. I may be a bit too late but my solution to this was to change the data-original-title

    $(‘.sample’).attr(‘data-original-title’: ‘new title’);

    This changes the bootstrap tool tip title automatically.

    Login or Signup to reply.
  6. Try following,

    $(document).ready(function(){
                $('#bag0').attr('title', 'new text')
                      .tooltip('destroy') // if you are using BS4 use .tooltip('dispose')
                      .tooltip({ title: 'new text'});
                $('[data-toggle="tooltip"]').tooltip('show');
    
    });
    <canvas id="bag0"  data-toggle="tooltip" title="test">
    </canvas>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    Login or Signup to reply.
  7. Change Bootstrap 4 Tooltip title

    $(document).ready(function() {
      // initilizing Tooltip
      $('[data-toggle="tooltip"]').tooltip();
    
      // Get the Tooltip
      let btn_tooltip = $('#my-btn');
    
      // Change Tooltip Text on mouse enter
      btn_tooltip.mouseenter(function () {
        btn_tooltip.attr('title', 'Default Tooltip').tooltip('dispose');
        btn_tooltip.tooltip('show');
      });
    
      // Update Tooltip Text on click
      btn_tooltip.click(function () {
        btn_tooltip.attr('title', 'Modified Tooltip').tooltip('dispose');
        btn_tooltip.tooltip('show');
      });
    });
    <!DOCTYPE html>
    <html lang="en-US">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container-fluid">
      <p>Click the button:</p>
      <button id="my-btn" type="button" class="btn btn-primary" data-toggle="tooltip" title="Default tooltip">Click Me</button>
    </div>
    
    </body>
    </html>
    Login or Signup to reply.
  8. I have a solution for similiar case. I change the title in tooltip dynamicaly with ajax.

    • first just enable the tooltips:

      $(‘[data-toggle="tooltip"]’).tooltip()

    • Then change the title attribute, then re init the tooltip

    $("#foo").attr('title','TheNewTitle');
    $('[data-toggle="tooltip"]').tooltip('dispose');
    $('[data-toggle="tooltip"]').tooltip()
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search