Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically update a tool tip when using materialize

I am using materializecss in my app script add-on, When I try and add a tooltip dynamically the tip does not update with the new label. I have tried several variations and even double-checked that the tip was being changed, which it was. The problem is that it does not seem to reinitialize with the updated text.

Here is a jfiddle: https://jsfiddle.net/edlisten/grafo4su/1/

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>

     <script>
         $(function() {
          $('.tooltipped').tooltip({delay: 50,tooltip:"new",position:"bottom"});
        });
        </script>
      </head>
      <body>

      <div class="container">
      <a class="btn tooltipped">Hover me!</a>
        </div>
      </body>
    </html>
like image 842
Bjorn Behrendt Avatar asked Feb 28 '16 11:02

Bjorn Behrendt


1 Answers

What you can do is set an id to the anchor. Lets say <a class="btn tooltipped" id="tooltip">Hover me!</a> Now we can find easily the element with jquery.

var anchorElement = $('#tooltip');

Materialize has an atribute data-tooltip you need to have on the element so that the tooltip will pop out.

anchorElement.attr('data-tooltip', 'New tooltip value');

And finally we need to initialize the materialize tooltip on the element

anchorElement.tooltip();

You can play around with it and put it in a function.

like image 156
Vasil Rashkov Avatar answered Jan 03 '23 20:01

Vasil Rashkov