Is there a way to show the text within a div tag in a tooltip without relying on jQuery?

Is there a way to display the text content of an HTML element inside a tooltip? I am struggling to achieve this, as I would like to have the word test appear in the tooltip, but it's not working.

Unfortunately, we are not using jQuery in our code base, so I can't rely on any jQuery plugins. However, we do write JavaScript code as needed. Here is my current setup:

The HTML div looks like this:

<div class="tooltip message">test</div>

Here is the CSS setup:

.tooltip {
    display: inline;
    position: relative;
}
.tooltip.message:before {
    background: #333;
    background: rgba(0, 0, 0, .8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    left: 20%;
    padding: 5px 15px;
    position: absolute;
    z-index: 98;
    box-sizing: border-box;
}
.tooltip.message:after {
    border: solid;
    border-color: #333 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 20px;
    content:"";
    left: 50%;
    position: absolute;
    z-index: 99;
}

Here is the link to my fiddle: http://jsfiddle.net/7LNJc/3/

Answer №1

Add an id to your class='tooltip message', for example:

<div class='tooltip message' id='test'>test</div>

Take a look at the following:

var doc = document;
function E(e){
  return doc.getElementById(e);
}

Now using E('test') is equivalent to document.getElementById('id'), so you won't need it anymore;

var test = E('test').innerHTML; // stores the html

If the Element has a value attribute, like an input, you would use:

var test = E('test').value; // stores the value of the html attribute

To update the innerHTML or value, you can do the following:

var test = E('test');
test.innerHTML = 'some text';

or

test.value = 'some value';

You can even simplify it to:

E('test').innerHTML = 'some text';

Answer №2

Experiment with using the CSS :hover selector on a div element instead of relying on the tooltip specified by the title attribute. This can provide a visually appealing tooltip effect.

div.tooltip {
  color: #ffffff;
}

div.tooltip:hover {
  border: 1px solid #000000;
  background-color: yellow;
  color: #000000;
}

Answer №3

Are you looking for a custom tooltip feature, or are you satisfied with the standard tooltip available in HTML? If the custom tooltip is not a requirement, you can simply use the title attribute to display a tooltip without the need for any JavaScript:

<div class="tooltip example" title="hello">hello</div>

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

Determine if a mobile application has been installed using Vue.js

I am currently developing a web application and have implemented a check to determine whether the user is accessing it from a mobile device or laptop. Let's consider the link as: my-site.com In addition to the web version, my site also offers a mobi ...

Aligning elements next to each other in html / css without using any top margins

How can I arrange four blocks of text side-by-side, center them over a background image, and ensure they respond well on different screen sizes without using tables? I typically use tables for this layout, but I've heard that CSS/HTML5 can achieve th ...

Tips for maintaining alignment of components within an Angular Material tab:

I am facing an issue with keeping items aligned properly. Initially, I had a form with two Angular Material lists placed side by side, each occupying 6 units and it looked good. However, when I tried to enclose these two lists within a material tab, they e ...

discovering the nearest preceding sibling that includes the class .myClass

I have multiple <tr> elements, some of which contain a <td> with the class class="myClass" and some do not. Here is an example of how it may look: <tr> <td class="myClass"></td> <td></td> </tr> <tr> & ...

Despite utilizing the here() package, Pandoc is unable to locate the image path referenced in an external HTML file that has been imported into R Markdown

As I create an HTML document with R Markdown, I incorporate external HTML documents for headers and footers. Utilizing the here() package enables relative paths within my project. However, including external HTML files via YAML in the R code disrupts the f ...

I am currently facing an issue with retrieving the class value that is being generated by PHP code

I am currently facing an issue with jQuery and PHP. Whenever I attempt to target the click event on the class ".id_annonce" in the dynamically generated PHP code, it doesn't retrieve the exact value of what I clicked. Instead, it always gives me a fi ...

When there is an absence of data, the jQuery datatable mysteriously van

I have encountered an issue in my Django app where a datatable used in the template disappears if there is missing data in any column or row. The problem occurs when trying to download the data in CSV, Excel, PDF, or copy format. Here is the HTML code snip ...

Encountered an error: Incorrect decomposition attempt on a non-iterable object

I have implemented a DataTable component using react-table. Below is the code for my component: const DataTable: React.FC<IDataTable<any>> = (props: IDataTable<any>) => { const { columns, data, className, rowSelection, onChange, ...r ...

The element does not have the 'ariaHidden' attribute present

I attempted to use the area-hidden property in a Vue file, but encountered an error. I also experienced a similar issue with the data-dismiss property. Do I need to perform additional setup to utilize the aria-hidden property? <button type="button& ...

Mysterious error arises in Internet Explorer versions 7 and 8: An expected colon is missing

One of our websites is encountering a puzzling JS error in Internet Explorer. The console displays the following message: ':' expected javascript:false, Line 1 Character 24 When attempting to trace the source of the error, a notification appear ...

How can I transform an HTML div into a video file like MP4 using Python and Django?

I'm looking to take a HTML page and target a specific <div> within it in order to convert it into video format. Purpose: I understand that HTML is typically static, but I have a requirement to transform it into a video. I'm seeking method ...

Getting the WatchPosition update just one time

I have implemented this code to continuously retrieve the first position and I need it to keep doing so. var init = function() { navigator.geolocation.getCurrentPosition(function(position) { new_position = position; }, onEr ...

Tips for creating multiple row tabs in Material UI Tabs

Currently, I have close to 30 tabs housed within Material UI Tabs. In order for the user to view all tabs, they must scroll two times. I am seeking a solution to display the tabs in two rows with a scroll, as opposed to one row with scroll. This would al ...

Show the ajax response on a separate page

I am looking to showcase the output of an ajax request on a separate page rather than the page where the ajax call originated. The scenario is that I have a membership directory page, and when a user clicks on a member ID cell, an ajax call sends the ID to ...

"Encountering a problem with a missing object in jQuery when referencing

I'm encountering an issue with jQuery's $(this) object that's causing me to lose track of the this element in my code: $('.star').click(function (){ var id = $(this).parent().attr('id').split('rating')[ ...

Tips for programmatically choosing dropdown menus in Angular 5

Trying to set up a dropdown select option in Angular 5 that dynamically changes based on the backend's input. https://i.sstatic.net/8cgsh.png Keep in mind that both select boxes are identical, only the options inside them vary. selector.html <h ...

How can I ensure that the state is only updated after the map function has finished executing in React?

I am encountering an issue with updating the state after mapping over an array of ids using an async function. The goal is to store the result in newArr and then update the state via setState. However, the state is being updated before the mapping operatio ...

The 'getGridMerchantLocationData' function contains duplicate definitions for both DataSource and DataSourceID. Please remove one of the definitions

I am having trouble binding data from a dataset. Can someone review my code and point out where I may be going wrong? The current issue is that it's not binding and throwing an exception: "Both DataSource and DataSourceID are defined on 'getGridm ...

Organizing your project with VueJS, VuelidateJS, and NodeJS/Express for optimal structure

Primarily, I specialize in developing intranet sites and web front-ends for embedded devices using NodeJS. Currently, all my code is contained within one NPM package. I have NodeJS running behind Nginx, allowing Nginx to serve css/image/client-side-javasc ...

Guide on setting up create-next-app with version 12 instead of the latest version 13

For an upcoming Udemy course, I am required to develop a Next.js project. However, it specifically needs to be built using Next.js version 12 rather than the latest version 13. Does anyone know how I can achieve this? ...