Adjust tool tip text on the fly

As a beginner, I created a test table and now I want to make the tool tip text change dynamically when I hover over the table with my mouse. Ideally, I would like the tool tip text to correspond to the content of the table cell. Currently, I only have static text in place. In the functionMouseOver() function, I need to retrieve the cell content and use it as the tool tip text. I am having trouble finding the JavaScript method to accomplish this. Below is the code that I currently have. Thank you for any assistance.

function functionMouseOver(id) {
  document.getElementById(id).style.backgroundColor = "rgb(0,255,255)";
}

function functionMouseOut(id) {
  document.getElementById(id).style.backgroundColor = "rgb(255,255,255)";
}

// create HTML Table body
titlenames = ["Jan", "Feb", "Mar"]
var HTMLTableBody = "";
HTMLTableBody += "<div class='container'><table id='id1' style='width:40%'>";
HTMLTableBody += "<tr>";
for (var i = 0; i < 3; i++) {
  HTMLTableBody += "<th id='th" + i + "' class='class1'>" + titlenames[i] + "</th>";
}
HTMLTableBody += "</tr>";
var cnt = 0;
for (var i = 0; i < 2; i++) {
  HTMLTableBody += "<tr>";
  for (var j = 0; j < 3; j++) {
    idstr = "td" + cnt;
    HTMLTableBody += "<td id='td" + cnt + "' class='class2'" +
      ' onMouseOver =' + '"functionMouseOver(' + "'" + idstr + "'" + ')"' +
      ' onMouseOut =' + '"functionMouseOut(' + "'" + idstr + "'" + ')"' +
      ' data-container =' + '"body"' +
      ' data-placement=' + '"right"' +
      ' data-toggle=' + '"tooltip"' +
      ' title=' + '"text"' +
      "></td > ";
    cnt++;
  }
  HTMLTableBody += "</tr>";
}
HTMLTableBody += "</table></div>";

document.getElementById("HTMLBody").innerHTML = "" + HTMLTableBody;

// fill table with data
cnt = 0;
for (var i = 0; i < 2; i++) {
  for (var j = 0; j < 3; j++) {
    var strid = "td" + cnt;
    document.getElementById(strid).innerHTML = cnt;
    cnt++;
  }
}

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
.class2 {
  background-color: rgb(255, 255, 255);
  color: rgb(0, 0, 0);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

.class1 {
  background-color: rgb(200, 100, 200);
  color: rgb(255, 255, 255);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

table {
  border-spacing: 1px;
  border-collapse: collapse;
  overflow: hidden;
  z-index: 1;
}

td {
  cursor: pointer;
  padding: 10px;
  position: relative;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body id="HTMLBody">
</body>

Answer №1

You have the option to modify functionMouseOver() in the following way, rather than setting all titles at the start:

function functionMouseOver(id) {
  document.getElementById(id).style.backgroundColor = "rgb(0,255,255)";  
  document.getElementById(id).setAttribute("data-original-title",document.getElementById(id).innerHTML);
}

function functionMouseOut(id) {
  document.getElementById(id).style.backgroundColor = "rgb(255,255,255)";
}

// Create HTML Table body
titlenames = ["Jan", "Feb", "Mar"]
var HTMLTableBody = "";
HTMLTableBody += "<div class='container'><table id='id1' style='width:40%'>";
HTMLTableBody += "<tr>";
for (var i = 0; i < 3; i++) {
  HTMLTableBody += "<th id='th" + i + "' class='class1'>" + titlenames[i] + "</th>";
}
HTMLTableBody += "</tr>";
var cnt = 0;
for (var i = 0; i < 2; i++) {
  HTMLTableBody += "<tr>";
  for (var j = 0; j < 3; j++) {
    idstr = "td" + cnt;
    HTMLTableBody += "<td id='td" + cnt + "' class='class2'" +
      ' onMouseOver =' + '"functionMouseOver(' + "'" + idstr + "'" + ')"' +
      ' onMouseOut =' + '"functionMouseOut(' + "'" + idstr + "'" + ')"' +
      ' data-container =' + '"body"' +
      ' data-placement=' + '"right"' +
      ' data-toggle=' + '"tooltip"' +
      "></td > ";
    cnt++;
  }
  HTMLTableBody += "</tr>";
}
HTMLTableBody += "</table></div>";

document.getElementById("HTMLBody").innerHTML = "" + HTMLTableBody;

// Populate table with data
cnt = 0;
for (var i = 0; i < 2; i++) {
  for (var j = 0; j < 3; j++) {
    var strid = "td" + cnt;
    document.getElementById(strid).innerHTML = cnt;
    cnt++;
  }
}

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
.class2 {
  background-color: rgb(255, 255, 255);
  color: rgb(0, 0, 0);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

.class1 {
  background-color: rgb(200, 100, 200);
  color: rgb(255, 255, 255);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

table {
  border-spacing: 1px;
  border-collapse: collapse;
  overflow: hidden;
  z-index: 1;
}

td {
  cursor: pointer;
  padding: 10px;
  position: relative;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body id="HTMLBody">
</body>

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

Nested CSS selector within a parent element

How can I change the color of both elements inside a custom button when the button is active? The first color is defined by the class of the button, and the second color is defined by the class of the span inside that button. CSS: .buttonClass{ color:b ...

Experiencing difficulties with the footer design in bootstrap framework

After creating a basic web page, I encountered an issue with the footer. No matter what adjustments I make, the text is not centered and it does not stay fixed when changing the browser height. To view the code snippet and preview, visit: ...

Ways to stop a tag from causing disruption to the alignment of another

In a div with text-align: center; applied, I have two tags. My issue is that I want one of them to be centered on the same line, while the other is positioned to the right. However, both tags are currently sharing the centered space and extending equally t ...

Adding an HTML arrow icon within a button

As I work on creating an HTML email, my aim is to incorporate a right arrow onto an HTML button. To achieve this, I have opted to utilize a special character and apply the transform-rotate property. However, I am facing an issue where the text and arrow a ...

How to detect a disconnected socket in Node.js using websockets

Encountering an issue with my nodejs websocket server. Upon graceful termination of client connections, the onclose method is triggered for closed sockets where I conduct clean up tasks. However, when clients disconnect due to network issues, the onclose ...

Is there a way I can detect a browser timeout and display a custom error message?

My webpage in classic asp contains a link to a local IP address, shown below: <a href="http://192.168.1.89">Link</a> When the local IP address is not available, the web browser eventually times out and shows its own error message. I ...

Validating Emails with Bootstrap and JQuery

Can anyone help me validate an email ID using Bootstrap? I've tried multiple methods, but it just doesn't seem to work. <label class=".col-xs-12 .col-sm-6 .col-lg-8" style="margin-left:-1%; margin-right:10%">Email</label> <input ...

What is the best way to create a React text box that exclusively accepts numeric values or remains empty, and automatically displays the number keypad on mobile devices?

While there are numerous similar questions on StackOverflow, none of them fully address all of my requirements in a single solution. Any assistance would be greatly appreciated. The Issue at Hand Within my React application, I am in need of a text box tha ...

Issue with JQuery event handlers becoming unresponsive upon resizing the window

JSBin link: http://jsbin.com/4QLDC/6 Here is the JS code snippet that I have written: var toggleContent = function (event) { $(event.target).siblings().toggle(); }; var showOrHidePanels = function () { var windowHeight = $(window).height(); v ...

Issue encountered while utilizing PHP sessions

I'm currently developing a basic login page that utilizes JS, JQuery, and PHP. login.php <!DOCTYPE html> <head> <title>AJAX Login Learning Activity</title> <link rel="stylesheet" type="text/css" href="login.css"> ...

Using AJAX to assign PHP session variables

Is there a way to update the SESSION variable named "fullname" on Page 2 without causing the page to refresh? This is my attempt using AJAX: HTML code for Page 1: <input type="text" name="fullname" id="fullname" placeholder="Full name"> <butto ...

The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual re ...

The event.js file is displaying an error at line 141, causing an unhandled 'error' event to be thrown

Trying to execute node 4.2.2 on a Mac OS is causing me some confusion as I keep encountering this error message: events.js:141 throw er; // Unhandled 'error' event ^ Error: spawn /Users/user/Documents/Projects/project-x/node_modules/ ...

Leverage Bootstrap modal to integrate with a separate form and database table

I have created a form that updates an SQL table named "invoices". Now, I am trying to implement a Bootstrap modal that will update another SQL table called "customers". The issue I am facing is that only the customer name is being saved properly. Is there ...

The dynamic concatenation of Tailwind classes is failing to have any effect, even though the full class name is being

I'm currently using Tailwind CSS within my Next.js project and I have a common method that dynamically returns the desired background color. However, despite adding the full class name, the background color is not displaying as expected. After reading ...

What is the best way to execute a function on certain text once the view has been refreshed?

Recently, I took over an AngularJS application that has presented a unique challenge for me. Within my page is a table of elements with values connected to the model using data-ng-bind. One specific element is data.name: <div class="component-name__pl ...

Personalizing the Twitter Embedded Timeline

I am curious about how much customization options are available for the Embedded Timeline. I searched through the documentation but still have some unanswered questions - maybe I missed something. My goal is to initially display only one tweet and then ha ...

Attempting to use jQuery AJAX to submit data without navigating away from the current webpage

Trying to implement the solution provided in this post where I am trying to send data to a PHP page and trigger an action, but unfortunately, it seems to just refresh the page without any visible outcome. Even after checking the network tab in the element ...

What is the best way to include a variable or literal as a value in styled components?

When it comes to managing various use cases, I always rely on props. However, I am currently facing a challenge in changing the border color of a styled input during its focus state. Is there a way to utilize props for this specific scenario? Despite my f ...

Having trouble uploading multiple input files in a Flask and HTML form

I'm attempting to upload multiple files using HTML in my Flask application. Below is the HTML form I have been using: <form action="url", method="POST", enctype="multipart/form-data"> <font size="2"> File2: <input type= ...