Disable the click functionality while preserving the hover feature

Due to my previous question being incorrectly marked as a duplicate, even though it wasn't, and no one is rectifying the mistake, I am re-posting my inquiry.

I am dealing with an <a> element that has a default onClick function. This <a> also has a hover effect that I want to maintain.

How can I disable the onClick function while retaining the hover effect?

P.s., I am particularly interested in a CSS-based solution!

Important note! My previous question was erroneously deemed a duplicate of this post: How to disable a link using only CSS?, but using pointer-events:none; also interferes with the hover effect.

If it is still being flagged as a duplicate, please link it to a question that is truly similar.


Edit: I neglected to mention that my hover effect is achieved in the following manner: https://jsfiddle.net/7o3dbak7/7/

HTML:

<div class="container">
  <ul>
    <li id="item1"><a href="somelinkthatistherebecauseitis">hoverable object</a></li>
    <li id="item2">text object, here comes alot of text explaining certain features of the website.</li>
  </ul>
</div>

CSS:

.container ul #item2 {
  display: none;
  width: 150px;
  padding: 20px;
  background: red;
  color: white;
  cursor: pointer
}

.container ul #item1 {
  pointer-events: none;
}

.container ul #item1:hover + #item2 {
  display: block;
}

Answer №1

If you want to disable interaction on the a element, you can use the CSS property pointer-events: none. To make the a element change color on hover, you can apply the hover effect to the parent element and target the a specifically within that:

span a {
  pointer-events:none;
}

span:hover a {
  color: red;
}
<span><a href="#">Hello, world!</a></span>

Answer №2

Instead of using an anchor tag, I recommend using a paragraph tag and adjusting your CSS accordingly.

Here is the CSS code:

.container #item2 {
    display: none;
    width: 150px;
    padding: 20px;
    background: red;
    color: white;
}

And here is the updated HTML code:

<div class="container">
    <p id="item1" href="somelinkthatistherebecauseitis">hoverable object</p>
    <div id="item2">text object, here comes a lot of text explaining certain features of the website.</div>
</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

Is there a way to detect changes in a Service variable within an Angular component?

One of my components contains a button that activates the showSummary() function when clicked, which then calls a service named Appraisal-summary.service.ts that includes a method called calc(). showSummary(appraisal) { this.summaryService.calc(appraisal ...

Connect tinyMCE to knockout.js

Learn how to utilize tinyMCE custom binding using. public sealed class CabinetShapeEditModel { public string Description { get; set; } } When implementing in the view: <script type="text/javascript"> var jso = @Html.Raw(Json.Encode ...

React - Why does React fail to update the state when expected? (not retaining)

Hello there, I'm currently working on fetching JSON data from an API and populating it into a table. It seems pretty straightforward but here's where things get tricky – I can see that the "tableData" state is getting updated as new rows are ad ...

The youtube API does not play nice with Jquery when it comes to handling ajax errors

I've been working on retrieving video data and search result data from the YouTube API using jQuery, but I'm having trouble with error handling in my code. I encountered an issue where a malformed video ID resulted in an error when testing it in ...

Discover the elements that are currently utilizing jQuery widgets

I currently utilize the jQuery-File-Upload plugin in my project, but I believe my query can apply to any jQuery plugin. The API documentation suggests initiating the plugin using the fileupload method, like so: $('#fileupload').fileupload(); My ...

Connecting Mailchimp with WordPress without using a plugin can lead to a Bad Request 400 error when trying to

I'm attempting to connect with Mailchimp using an Ajax request without relying on a plugin, but I keep encountering a 400 bad request error. The code provided below utilizes vanilla JS for Ajax and the function required for integration with Mailchimp. ...

When text is inserted into an inline-block div, it causes the div to shift downwards in

Consider the following HTML code snippet: <div class="one"> Test </div> <div class="two"> </div> <div class="three"> </div> If we apply the following CSS styles: div { display:inline-block; height:30px; ...

Ways to eliminate line breaks in CSS

I'm currently working on a project and I'm facing an issue with text auto-entering without using any breaks. I'm not sure what the exact problem is. I implemented a code snippet from Overstack to display a div on hover. Perhaps the positioni ...

Tips for locating a specific value within an array using ReactJS

I have a set of dates provided by the back-end that I need to work with in order to perform a specific check. If the list contains dates from the month of March, I should display the events for March, and the same for April, and so on. My task involves se ...

Generate a distinct identifier for the select element ID whenever a new row of data is inserted into a table

Although my title accurately describes my issue, I believe the solutions I have been attempting may not be on the right track. I am relatively new to javascript and web development in general, so please forgive me for any lack of technical terminology. Th ...

What is the process for sending tinyEditory's content using ajax?

I recently incorporated the StfalconTinymceBundle into a Symfony2 project and it is functioning perfectly. However, I am encountering a problem when trying to submit the editor's content through AJAX. The editor's content does not seem to be bind ...

Tips on implementing a circular progress bar with locomotive scroll functionality

Can anyone help me integrate progress functionality with Locomotive Scroll using JavaScript? Link to CodePen for reference Check out this code snippet from Locomotive Scroll that calculates the percentage of pages scrolled: const scroller = new Locomotiv ...

Attempting to achieve CSS shadows on both the right and left sides of the element

I am struggling to apply a CSS box shadow to a DIV element. Specifically, I want the shadow to appear only on the left and right sides of the element. I have tried using the following code: box-shadow: 0 0 10px #000; However, despite my efforts, the shad ...

How can AngularJS service methods be assigned to controller $scope for use in an ng-repeat loop in the view?

As part of my development process, I decided to refactor my controller code in order to make it more reusable across multiple controllers. The original version of the controller (colorController.js) worked perfectly fine before I attempted to refactor it i ...

Guide to building a hierarchical data object with JavaScript

Prior This object consists of multiple rows: { "functions": [ { "package_id": "2", "module_id": "2", "data_id": "2" }, { "package_id": ...

Choose just one button from the variety of buttons available

I am facing an issue with a component that contains multiple cards, each with its own button. Below is the code for this component: import React, { useState, useEffect } from "react"; import "./App.css"; import { Card, Container, Row, C ...

How to Store Values from a ReadStream in an Array

I'm currently attempting to use a stream and the async/await keywords with the fast-csv library in order to read a CSV file synchronously. Unfortunately, the function I've written doesn't seem to be producing the expected output. My assumpt ...

Error encountered while compiling NextJS: Unexpected use of single quotation mark in jsx-quotes

I can't seem to compile my NextJs 13 app without encountering errors. Take a look at my shortened eslintrc.js file below: module.exports = { env: { browser: true, es2021: true, }, extends: [ 'plugin:react/recommended', ...

The CSS file is not displaying the background image on the PHP file

I am facing an issue with using CSS to include a background image for the layer, but it doesn't seem to be working correctly... The CSS code snippet is as follows: header { background-image: url("boisko.jpg"); background-repeat: no-repeat; background ...

Using API call responses to initiate a render in React

As a newcomer to React, I'm facing an issue with passing the results of my API call to another component file in order to trigger a render. Can anyone provide a straightforward explanation of what steps I need to take? In my code, I make a call to th ...