Tips for activating a tooltip upon clicking instead of hovering over it:

I am currently working with tooltips and I am looking to display the tooltip message upon clicking, rather than on hover.

Is there a way to make this modification?

This is the code I have so far:

.tooltip {
  position: absolute;
  top: 7px;
  margin-left: 10px;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 570px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}
<input type="text" maxlength="8" />
<a href="#" class="tooltip">
  <img src="~/images/help-80.png" />
  <span class="tooltiptext">
     <img src="~/images/keyboard.png" /><br />
     "This is the absolute maximum size of your item. Don't worry about different configurations here."
    </span>
</a>

Currently, the message only appears when hovering over the icon. However, I would like it to be displayed on click and remain open.

Answer №1

To change the style of an element, you can inspect the style property and apply a new style when clicking on an image.

$('.tooltip').click(function(){
  var el = $('.tooltiptext');
  el.css('visibility') == 'visible' ? 
    el.css('visibility','hidden') : 
    el.css('visibility','visible');
});
.tooltip {
  position: absolute;
  top: 7px;
  margin-left: 10px;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 570px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" maxlength="8" />
<a href="#" class="tooltip">
    <img src="~/images/help-80.png" />
    <span class="tooltiptext">
        <img src="~/images/keyboard.png" /><br />
        "This is the maximum size your item can be. Different configurations are accounted for here."
    </span>
</a>

Answer №2

$(".tooltip").on("click", function(){
   $(".tooltiptext").toggle();
});

Answer №3

Make a change from displaying .tooltiptext on hover to showing it on focus.

.tooltip:focus .tooltiptext {
    visibility: visible;
}

However, if the image is focused using the keyboard, the tooltip will still appear. To prevent this behavior, jQuery can be used.

.tooltip {
  position: absolute;
  top: 7px;
  margin-left: 10px;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 570px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
}

.tooltip:focus .tooltiptext {
  visibility: visible;
}
<input type="text" maxlength="8" />
<a href="#" class="tooltip">
  <img src="~/images/help-80.png" />
  <span class="tooltiptext">
    <img src="~/images/keyboard.png" /><br />
    "This is the absolute maximum size of your item. Don't worry about different configurations here."
  </span>
</a>

Answer №4

Make a modification to your CSS by eliminating the visibility: hidden; property and also remove the Class .tooltip:hover .toolt. Instead, display or hide your tooltip based on a click event.

$('.tooltiptext').hide();
$('.tooltip').click(function(e) {
  $('.tooltiptext').show();
});
.tooltip {
  position: absolute;
  top: 7px;
  margin-left: 10px;
}

.tooltip .tooltiptext {
  width: 570px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" maxlength="8" />
<a href="#" class="tooltip">
  <img src="~/images/help-80.png" />
  <span class="tooltiptext">
                                <img src="~/images/keyboard.png" /><br />
                                "This is the absolute maximum size of your item. Don't worry about different configurations here."
                            </span>
</a>

Answer №5

Another option is to utilize the bootstrap popover feature found at this link. By implementing this, you will achieve the desired outcome.

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

Concealing the text input cursor (caret) from peeking through overlaid elements on Internet Explorer

Currently, I am facing an issue with a form that includes a unique widget. This particular widget automatically populates a text input when it is in focus. The problem arises when the widget appears above the text input as intended. In Internet Explorer ...

Utilizing Express.js for setting up routes with a default path

In the code snippet below, I am utilizing Express.js: router = express.Router() fs.readdirSync('./controllers').forEach(function (file) { if(file.substr(-3) == '.js') { route = require('./controllers/' + file); ...

Incorporating JSON data into data-groups within an Ionic application

I am currently working on developing an Ionic app using the newest version (Ionic 3xx and Angular 5) and I am attempting to incorporate the shuffleJS feature for filtering. Everything was working fine when I used static data in my template with the 'd ...

Is there an alternative method to achieve the desired result without relying on the map() array function? What specific benefits does using the map() array method offer in this context?

To find the source content, please visit this link: https://github.com/LinkedInLearning/javascript-essential-training-2832077/tree/main/08_17. The specific code in question can be seen below: import backpackObjectArray from "./components/data.js" ...

Tips for swapping a component with another component in React.js without the need for a page refresh

class Navigation extends Component { constructor(props) { super(props); this.state = { width: window.innerWidth, } } updateWidth = () => { if (this.state.width > 700) { this.setStat ...

Could the slow loading time of the React site be attributed to an overload of static assets?

As a ML professional diving into frontend development, I have recently incorporated various fixed assets such as images into the assets folder for React. However, I've noticed that my website is running slower than expected. Do you believe that these ...

Steps to dynamically modify a class upon clicking

Imagine having a sequence of div elements: <div id='d-1' class='hidden'>this one<br /> <button onclick='next();'>Go to Next</button> </div> <div id='d-2' class='show'&g ...

Troubleshooting: Why is $watch failing to track changes on factory variables in Angular

I have created a factory named SharedService as shown below: angular.module('Fms').factory('SharedService', function() { var userdetails=false; return userdetails; }) Below controllers utilize this factory: angular.mod ...

Ways to modify the jquery script to display on page load rather than on scrolling action

Currently in the process of working on my site, www.runebs.dk. Utilizing a script to activate information for each project. JS.. $('.showHide').on('click',function(){ $(this).toggleClass('active'); $( '#subHead ...

Issue with Array.push() method resulting in undefined return value

Currently, I am in the process of fetching data from an API to later use it for creating a visualization using Chart.js. However, I've encountered a problem while trying to store this retrieved data in arrays and then encountering undefined values whe ...

Issues with Safari not recognizing media queries

Can anyone provide some guidance on why these media queries are not working in Safari? I've tested them in Chrome and they work fine. Any help would be greatly appreciated as I'm new to coding. @media screen and (max-width: 768px) { #wellLogo ...

Having trouble extracting data from a particular cell within an HTML table using jQuery

I am struggling to extract row data from a table for utilization in a modal. Provided below is my code for Views. <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css"> <link href="@Url.Content(" ...

Update the page by selecting the refresh option from the drop-down menu

In my script, I have different views showing information retrieved from a database. One view displays the quantity of a product sold each month, another shows sales, the third presents profits, and the last exhibits calculated percentages using sales and p ...

The animated sticky header lacks smooth animation while scrolling upwards

This dynamic menu transitions the logo to the left and the navigation to the right as you scroll down. The animation is smooth when scrolling down, but slightly jerky when scrolling up, especially with the navigation. The animation uses CSS3: transition: ...

What is the process of retrieving data in PHP from $.post and storing it in a $variable?

After searching extensively, I have yet to find a solution that works for my specific issue of form posting using $.post to a php file to save data into a csv file. The formData is serialized in a separate function and stored in a constant for local stora ...

Mastering the art of tab scrolling in VueJS using Bootstrap-vue

Currently, I am working on a VueJS project that utilizes bootstrap-vue. The issue I am facing is that when rendering a list of tabs, it exceeds the width of its parent container. To address this problem, I aim to implement a solution involving a set of but ...

The easeInBounce function is not functioning as intended

I am currently working on a slide-down function that successfully slides text when the page is loaded. However, I am encountering an issue when trying to edit the transition effect as it seems to ignore my changes... Below are the JS links I have added: ...

Retrieve the row from the table using the "let" keyword

I'm attempting to retrieve the row of a table by selecting a radio button. My goal is to identify the selected row in order to access the experiment ID, but when I try this, the alert shows "Row index is: undefined." I found the code at: https://www. ...

Utilizing dual <legend> elements within a fieldset

I am attempting to include two legends within a fieldset. The first legend consists of an image in the center of the fieldset, while the other legend should be another image aligned to the very right side of the fieldset. The code snippet below demonstrate ...

What is the best way to locate an element using text in xpath?

Having an issue with this HTML source code: <td class="specs_title"> Processortype <a href="#" class="info-link"> <img src="x.jpg" title="" height="16" alt="" width="16" /> <span class="info-popup"> <span class="hd">Processor ...