I would like to display a div when the mouse cursor hovers over a specific link, and then hide that div when the mouse cursor moves away from the link.
I would like to display a div when the mouse cursor hovers over a specific link, and then hide that div when the mouse cursor moves away from the link.
give this a shot
<script type="text/javascript">
function displayContent(divID) {
var element = document.getElementById(divID);
if (element) {
element.className='visible';
}
}
function hideContent(divID) {
var element = document.getElementById(divID);
if (element) {
element.className='invisible';
}
}
</script>
<style type="text/css">
.invisible {
display: none;
}
.visible {
display: block;
}
</style>
<p><a href="javascript:void(0);" onmouseover="displayContent('test1');" onmouseout="hideContent('test1')">Toggle Content</a></p>
<div id="test1" class="invisible">
testing
</div>
Check out this alternative solution that doesn't rely on jQuery. While jQuery can achieve the same outcome in fewer lines of code, this approach uses JavaScript to dynamically set CSS styles and add event handlers. By leveraging JavaScript's capabilities, we aim to keep the HTML unobtrusive. Feel free to view a working demo of this solution through this live example link.
Here's how the code is structured:
<a id="myMagicLink" href="http://www.google.com/">My Magic Link</a>
<div id="openingDiv">Click to reveal a hidden div</div>
And here's a snippet of the CSS used for styling:
#openingDiv {
background-color: #ffc;
border: 1px solid;
padding: 0.5em;
display: none;
position: absolute;
left: 100px;
top: 100px;
}
JavaScript functions are employed to add event listeners:
function registerEvent(element, eventType, callback) {
if (element.addEventListener) {
element.addEventListener(eventType, callback, false);
} else {
element.attachEvent('on' + eventType, callback);
}
}
registerEvent(window, 'load', function() {
var link = document.getElementById('myMagicLink');
var div = document.getElementById('openingDiv');
registerEvent(link, 'mouseover', function() {
div.style.display = 'block';
});
registerEvent(link, 'mouseout', function() {
div.style.display = 'none';
});
});
In the traditional way:
<a href="#" id="foo" onmouseover="togglediv();" onmouseout="togglediv();">Toggler</a>
<div id="bar">Content</div>
And the corresponding JavaScript:
togglediv = function() {
var mydiv = document.getElementById('bar');
mydiv.style.display = mydiv.style.display == '' ? 'block' : '';
}
If you prefer not to clutter your HTML with inline event handlers (and it's recommended not to), you can use a different approach like this:
function setupHover(id){
var el = document.getElementById(id);
if (el.addEventListener) { // for modern browsers
el.addEventListener ('mouseover',togglediv,false);
el.addEventListener ('mouseout',togglediv,false);
} else if (el.attachEvent) { // for IE
el.attachEvent ('onmouseover',togglediv);
el.attachEvent ('onmouseout',togglediv);
} else { // for other browsers
el.onmouseover = togglediv;
el.onmouseout = togglediv;
}
@idealmachine demonstrates a clever way to encapsulate this in a listener function in their response.
Then you can call setupHover('foo')
in your onload
function:
window.onload = function(){
setupHover('foo');
// perform other tasks
};
Alternatively, if you are using a library (like jQuery), you can achieve the same goal while abstracting the cross-browser discrepancies:
$(document).ready( function(){ // when the DOM is loaded
$('a#foo').hover( // listen for hovers on the <a>
function(){
$('#bar').toggle(); // toggle visibility directly
// $('#bar').toggleClass('visible'); // or toggle a class for the same effect
}
);
});
This is perhaps the simplest method, although you will need to include jQuery or your chosen equivalent library.
All the methods assume that your div is initially hidden:
<style type="text/css">
div#yourdiv { display: none; }
</style>
While I know this question has been asked multiple times before, I still haven't been able to find a solution. My goal is to take an image, rotate it, and place it in an HTML canvas. To achieve this, I am utilizing another canvas where I rotate the i ...
The issue at hand My parent component (X) is responsible for managing numerous states and child components. Within these children, there is an animated component (Y) - an avatar with various facial expressions that change in sync with its dialogue. Curr ...
Every time I try to add some styles to my ButtonWidget.scss file, I encounter an error in the console when running the command yarn storybook Error Code: ERROR in ./src/components/ButtonWidget/ButtonWidget.module.scss 1:0 Module parse failed: Unexpected ...
One challenge I am facing involves a dropdown list with two options. Each option, when selected, should change the display of content in the view. How can I connect my dropdown selection to show one content and hide the other? This is what I currently hav ...
I want to automatically submit a form at a specific time of day The form should be submitted every day at "15:30:00" Here is the JavaScript code I have written: <script type="text/javascript> function initClock() { var now = new Date(); var h ...
After separately creating an express-based backend (in folder A) and a react-based front-end project (in folder B), I decided to merge them, placing the front-end project inside the back-end project for several advantages: No longer do I need to manu ...
At the moment, I have several functions that return promises like the example below: function action_one(){ return new Promise((resolve, reject)->{ ... }); } I am looking for a way to wait for one promise to finish before moving on to t ...
Suppose there is a JavaScript page located at myaddress/service.js on a server. The code in this .js file looks something like: nsBob = { a: function(someParam) {...perform actions and return result}, b: function() {...perform actions and return result} ...
Currently, I am facing an issue with a card section that contains two buttons and a description. The first button adds an image which is working perfectly fine, as well as the description section. On the other hand, the second button adds a video and when ...
I am currently working on developing a chat application. The first step involves the user sending their username, and if the name does not already exist, they are logged in. The user can then send a message to another user. However, an issue arises where a ...
When you examine TypeScript's async function, you may notice the redundancy with "async" and "Promise<type>". public async test(): Promise<string> { return "Test"; } Is there a way to configure TypeScript to handle async types ...
Attempting to run multiple Node.js apps on a single server has been my focus lately. I have been exploring solutions for similar queries here and have reached some progress. Let's consider the scenario where I have two apps, each serving different HTM ...
I am attempting to create a menu with sub-menus. The desired structure for my menu is outlined below. However, the demo I'm testing is not producing the correct structure. Check out the demo here. "Sub Test": { // Main menu "Example1":"hai",//sub ...
I'm looking for a way to display something at its actual size on a browser. For instance, if something appears to be 20cm on the screen, I want it to truly measure up to 20cm. Some methods involve considering the diagonal resolution of the screen, li ...
As I delve into the code of a Node.js Express application for learning purposes, I came across the following line that sparked my curiosity regarding the inclusion of getters and virtuals. var pgmsDbObj = chnnlList[chnnlIndex] var pgmsObj = pgmsDbObj.to ...
In this sample data, I am looking to filter an object based on its ID key let myData = { "nodeInfo": { "9": { "1": { "ID": "14835" ...
In my view servlet, I am displaying user data from the database in a table. Each row of the table has buttons that allow users to change the cells in that row to text boxes. The issue I am encountering is that when I retrieve the data and loop through to ...
Using span and ng-repeat, I have created tags that trigger a modal pop-up when the remove button is clicked. Within this modal, there is a delete button that calls a function. I am trying to figure out how to pass the id of the remove button to the modal ...
https://stackblitz.com/edit/vue-script-setup-with-vuex-gfjaff?file=src/components/UserProfile.vue I'm currently working on a feature where I can input a name into a dispatch function and have it added to an array. Eventually, I plan to include a text ...
Every time I run truffle test in the terminal, I encounter the following error message: Error: Attempting to run a transaction which calls a contract function, but the recipient address 0x3ad2c00512808bd7fafa6dce844a583621f3df87 is not a contract address. ...