Search code examples
javascripthtmlcsstooltip

how to change data-tooltip message


is there a way i can control the data-tooltips message by using javascript? like html dom ?

/* Add this attribute to the element that needs a tooltip */

[data-tooltip] {
  position: relative;
  z-index: 2;
  cursor: pointer;
}
/* Hide the tooltip content by default */

[data-tooltip]:before,
[data-tooltip]:after {
  visibility: hidden;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
  opacity: 0;
  pointer-events: none;
}
/* Position tooltip above the element */

[data-tooltip]:before {
  position: absolute;
  bottom: 150%;
  left: 50%;
  margin-bottom: 5px;
  margin-left: -80px;
  padding: 7px;
  width: 160px;
  border-radius: 2px;
  border: 1px outset #C0C0C0;
  box-shadow: 3px 2px 5px #9F9F9F;
  background-color: #000;
  background-color: hsla(206, 73%, 34%, 0.9);
  color: #FFFFFF;
  content: attr(data-tooltip);
  text-align: center;
  font-size: 11px;
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  font-weight: bold;
  line-height: 1.2;
}
/* Triangle hack to make tooltip look like a speech bubble */

[data-tooltip]:after {
  position: absolute;
  bottom: 150%;
  left: 50%;
  margin-left: -5px;
  width: 0;
  border-top: 5px solid #000;
  border-top: 5px solid hsla(206, 73%, 34%, 0.9);
  border-right: 5px solid transparent;
  border-left: 5px solid transparent;
  content: " ";
  font-size: 0;
  line-height: 0;
}
/* Show tooltip content on hover */

[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
  visibility: visible;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
  opacity: 1;
}
 <a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/  ></a>  


Solution

  • Sure you can. The attribute text taken from the attribute data-tooltip using (content: attr(data-tooltip)).

    So, you just need to change the attribute's value to whatever you want.

    function changeTooltip() {
      document.getElementById('abc').setAttribute('data-tooltip', 'aaa');
    }
    /* Add this attribute to the element that needs a tooltip */
    
    [data-tooltip] {
      position: relative;
      z-index: 2;
      cursor: pointer;
    }
    /* Hide the tooltip content by default */
    
    [data-tooltip]:before,
    [data-tooltip]:after {
      visibility: hidden;
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
      filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
      opacity: 0;
      pointer-events: none;
    }
    /* Position tooltip above the element */
    
    [data-tooltip]:before {
      position: absolute;
      bottom: 150%;
      left: 50%;
      margin-bottom: 5px;
      margin-left: -80px;
      padding: 7px;
      width: 160px;
      border-radius: 2px;
      border: 1px outset #C0C0C0;
      box-shadow: 3px 2px 5px #9F9F9F;
      background-color: #000;
      background-color: hsla(206, 73%, 34%, 0.9);
      color: #FFFFFF;
      content: attr(data-tooltip);
      text-align: center;
      font-size: 11px;
      font-family: "Trebuchet MS", Helvetica, sans-serif;
      font-weight: bold;
      line-height: 1.2;
    }
    /* Triangle hack to make tooltip look like a speech bubble */
    
    [data-tooltip]:after {
      position: absolute;
      bottom: 150%;
      left: 50%;
      margin-left: -5px;
      width: 0;
      border-top: 5px solid #000;
      border-top: 5px solid hsla(206, 73%, 34%, 0.9);
      border-right: 5px solid transparent;
      border-left: 5px solid transparent;
      content: " ";
      font-size: 0;
      line-height: 0;
    }
    /* Show tooltip content on hover */
    
    [data-tooltip]:hover:before,
    [data-tooltip]:hover:after {
      visibility: visible;
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
      filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
      opacity: 1;
    }
    <a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/  ></a>
    <br />
    <button onclick="changeTooltip()">Change tooltip to "aaa"</button>