Toggle the tooltip to reveal a hidden div by clicking, and then click again to close it

I've been working on implementing a toggle div with a tooltip. The issue I'm facing is that the tooltip initially displays "Click to open", but after opening the toggle, it should change to "Click to close". Can someone assist me in achieving this functionality?

If you'd like to take a look at my project, you can download the zip file here.

You can also view the project live by clicking on this Live Link.

$(document).ready(function(){
    $(".latestnews").click(function(){
        $(".latestnews_toggle").toggle(1000);
    });
});

I would greatly appreciate any help in fixing the tooltip to display the appropriate message for opening and closing. It should show "Click to open" when closed and "Click to close" when opened.

Answer №1

The simplest method is to introduce a "status" variable such as var widgetIsOpen = false. To toggle the state of the widget, just use widgetIsOpen = !widgetIsOpen; and update the text accordingly based on the variable's value.

If you need to dynamically assign a message, consult the documentation:

<script>
  var widgetIsOpen = true;
  $(document).ready(function(){
     $(".latestnews").click(function(){
        widgetIsOpen = !widgetIsOpen;
        $(".latestnews_toggle").toggle(1000);
     });
  });
  $(document).ready(function() {
    $('.rgttool').tinytooltip({
       message: function(tip) {
          if(widgetIsOpen===true) {return "Click to close";}
          return "Click to open";
        }
     });
  });
</script> 

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

Encountering an error in Express while attempting to upload an image due to the inability to read the property 'file' of undefined

I am currently learning Express framework. I encountered an error while trying to upload an image using Express. The error message I received is "Cannot read property 'file' of undefined." Below are the code snippets that I am using, and I&apo ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

Validation of dynamically generated name fields using radio button group

CODE: html <form id="myform" type="post"> <fieldset id="myid1"> <input id="entries_8490_burn_id_1" name="entries[8490][burn_id]" value="1" type="radio"/> <input id="entries_8490_burn_id_2" name="entries[8490][burn ...

Retrieve only the names of keys from a JSON object presented in array form using Node.js

How can I retrieve JSON data key names and store them in an array using nodejs? I attempted the following code, but it returned an object instead of an array. I need the key names in an array format so that I can save them in a variable. const jsondata = ...

"Can someone provide guidance on properly aligning an image in a div

My deadline for my year 11 task is just one day away and I've tried multiple solutions, but it seems like I might have implemented them incorrectly in my code. I really need help! Unfortunately, I don't have a fiddle example to share because I&ap ...

(codesandbox) Is there a way to have both the label 'checkbox' and the first option pre-checked and pre-selected?

When working with ReactJS, I am currently in the process of mapping some data. My goal is to have all the label checkboxes and their initial values checked by default. Additionally, I want to implement a feature where unchecking a checkbox will also unchec ...

Issue with vertical cell alignment in MUI x-data-grid persists following the latest update to version 7.2.0

After updating my MUI app to the latest npm packages version, specifically upgrading x-data-grid from 5.17.9 to 7.2.0, I encountered an issue. In my application, I utilize a grid where certain columns are populated using the renderCell property: const cel ...

Encountering a "Cannot modify" error in wp-admin while using inspect element

It seems like there is a slight error appearing in the Inspect Element Source Tab of Google Chrome (also checked in Firefox). I have searched extensively for a solution but haven't found anything helpful. My Wordpress theme displays wp-admin in inspec ...

Streaming live video on the website

Hi there! I'm looking to integrate live video capturing into an HTML/JavaScript site for a presentation. However, I'm not sure where to start my search. Can anyone point me in the right direction? :) The live video will be captured by a camera o ...

Is it possible to set up VS Code's code completion feature to automatically accept punctuation suggestions?

For all the C# devs transitioning to TypeScript in VS Code, this question is directed at you. I was captivated by the code completion feature in VS C#. To paint a clearer picture, let's say I'm trying to write: console.log('hello') W ...

Django is causing a TemplateSyntaxError while attempting to extend the "template.html" file with the static keyword

In my template.html file, I have included a lot of boilerplate HTML text. Now, in my index.html file, I have: {% load static %} {% extends static 'template.html' %} Within my settings.py file (relevant parts): INSTALLED_APPS = [ 'notes ...

Sending a string of HTML elements to a Vue custom directive is causing problems with the eslint code analysis

I have developed two custom Vue directives to add an HTML element before or after another element. I provide an HTML string to the element where I apply the directive, but I am encountering an error in VS Code: Parsing error: unexpected-character-in-a ...

Creating a Responsive Design with Bootstrap 4 - Adjustable Height Structure

I am facing an issue with my website built on Bootstrap 4. I am trying to make a layout that utilizes the full height of the screen. I had expected the new "flex" grid system in Bootstrap 4 to facilitate this, but it seems like I might be implementing it i ...

Integrating Symfony2 with Twig while passing a variable in a controller

I've been diving into symfony2 and I've hit a roadblock for the first time. I'm working on creating 3 pages: an index page, a product page, and a special offer page. All of these pages need to share a dynamic sidebar from another template. ...

Aligning the navigation links vertically

I am working on aligning my navigation bar vertically, even when I scroll the page. I found a method in another thread which suggests using the following CSS code for vertical alignment: #container { position: absolute; top: 50%; height: 400p ...

Could you explain the distinction between Node.bind() and Template Binding?

Currently, I am exploring the documentation for Google Polymer and have come across two types of data binding - Node.bind() and Template Binding. Can someone please explain the distinction between Node.bind() and Template Binding to me? ...

Creating a Functional Right Arrow on a Pure CSS Select Menu/Dropdown

I have implemented a custom select/dropdown menu by following the solution provided here: The functionality works well, but I am facing an issue where the dropdown options only appear when clicking on the box. Clicking on the 'arrow' located on ...

Rendering in Three JS involves efficiently utilizing one buffer to display the output within itself

I have been struggling with a particular issue and I really need some assistance: In my three js context, I have created a custom material and rendered it into a texture. ` /* Rendering in texture */ fbo_renderer_scene = new THREE.Scene(); fbo_r ...

Turn on/off the button click functionality on a jQuery smart wizard control

I am currently utilizing this particular control, which happens to be version 3 of jQuery Smart Wizard. Here is the code snippet for the smart wizard: $('#wizard').smartWizard({transitionEffect:'slide'}); These are some helper functi ...

Express bodyParser may encounter difficulties in parsing data sent from Internet Explorer

After creating a server app with Node and Express 4, I utilized jQuery for the front-end. An Ajax call was set up to send data via POST to the server: $.ajax({ cache: false, type: 'POST', url: Config.API_ENDPOINT_REGISTRATION, ...