Disable hover effects in CSS with JavaScript

I am looking for a way to deactivate CSS hover functionality using JavaScript.

Within a form, there is a button that sends data to the database server. By utilizing OnClientClick() of an ASP.NET Button control, my goal is to modify the text of the element to 'Submitting..', change the background color of the button to Light Grey and most importantly, eliminate the hover effect defined in my CSS for .button:hover, .content-form input.button:hover, and #comment-form #submit:hover.

.button:hover, 
.content-form input.button:hover, 
#comment-form #submit:hover   
{
    background-color: #333;
}

The main focus is on implementing Javascript to disable the aforementioned CSS effects.

e.g. OnClientClick="getElementByID('ButtonName').blahblahblah;"

Answer №1

Here is a functional demo: https://jsfiddle.net/abc123xyz/25/

const elements = document.querySelectorAll(".sample");
const count = elements.length;
function modifyStyle(style) {
    for(let i = 0; i < count; i++) {
        elements[i].style.color = style;
    }
}
for(let i = 0; i < count; i++) {
    elements[i].onmouseover = function() {
        modifyStyle("blue");
    };
}

Note: Apologies for missing the final part of your query in my initial response :)

Answer №2

There are numerous ways to tackle the issue at hand.

For instance:

1- Utilizing the HTML disabled attribute.

OnClientClick="getElementByID('ButtonName').disabled=true;

2- Introducing a class that overrides the existing style.

.button:hover, 
.content-form input.button:hover, 
 #comment-form #submit:hover    
 {
    background-color: #333;
 }

 .button.submitted:hover
 {
    background-color: gray;
 }

Js:

OnClientClick="getElementByID('ButtonName').className = "submitted";

and so on

Answer №3

In this scenario, it first removes the class attribute which eliminates all previously defined classes and then adds a new class that should not be removed.

Check out the code on jsfiddle

function eliminateClass(elem) {
elem.removeAttribute('class');
elem.setAttribute('class', 'another');
}
.button:hover {
    background-color: #333;
}

.another {
    background-color: lightgray;
}
<button class="button" onclick="eliminateClass(this);">hover</button>

However, the most effective way to achieve this is much simpler and efficient.

function disableHover(elem) {
    elem.classList.remove('button');
    elem.classList.add('another');
}
.button:hover {
    background-color: #333;
}
.another {
    background-color: lightgray;
}
<button class="button" onclick="disableHover(this);">hover</button>

See the improved code at jsfiddle

Answer №4

First and foremost, the css you have written is incorrect. It should be:

.button:hover, .content-form input.button:hover, #comment-form, #submit:hover    {
    background-color: #333;
}

Additionally, you are applying css with both id and class. This is not recommended. Stick to using classes only and consider using

document.getElementById('submit').removeAttribute('class')
instead.

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

Dealing with promises in AngularJS within the ui-router configuration

Below is a snippet of my $stateProvider code: $stateProvider .state("home", { url: "/", template: "<employee-info-component user='$resolve.user'></employee-info-component>", resolve: { user: function(indiv ...

Managing numerous ajax forms within a single page

Upon receiving advice from the following inquiry Multiple forms in a page - loading error messages via ajax, I am endeavoring to incorporate multiple forms within one page. The goal is to insert error messages into the corresponding input div classes. I pr ...

What are the steps for implementing a Custom SiteMapProvider in ASP NET MVC?

Currently, I am working on implementing a Custom SiteMap Provider. I have gone through various tutorials on the subject and followed their instructions. As per the guidelines, I have created a subclass of XmlSiteMapProvider named MySiteMapProvider, which ...

Learn how to pass an id from the query parameters to the getInitialProps function in Next.js

Looking to create a basic website that displays content from a database? Utilizing the getInitialProps function from Next.js documentation can help with server-side rendering: https://nextjs.org/docs/api-reference/data-fetching/getInitialProps#getinitialpr ...

Having trouble with my ASP.net SQL UPDATE query, it's not functioning as expected

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); conn.Open(); var session_username = Session["user"]; string sql1 = "UPDATE Users SET email='" + newemail.Text + "' WHERE username ...

The value control input does not get properly updated by ngModelChange

Having difficulty updating an input as the user types. Trying to collect a code from the user that follows this format: 354e-fab4 (2 groups of 4 alphanumeric characters separated by '-'). The user should not need to type the '-', as it ...

Is there a distinction between em and percentage measurements and do their defaults have specified definitions?

Both em and percentage are defined in relation to other elements or properties. For example, with text-indent, it is related to the width of the paragraph, while with font size, it is related to the browser's default size. The question arises - wh ...

Dynamically inserting a new row into a table with the power of jQuery

Currently working on dynamically adding rows to a table through jQuery in my project that follows MVC design pattern. I have set up a loop for the added rows, but need help with the script. Here is my code snippet for the loop : <?php $viewTableR ...

Are there equivalent npm variables like (`npm_config_`) available in yarn console scripts?

Utilizing variables in custom npm commands is possible (example taken from ): { "scripts": { "demo": "echo \"Hello $npm_config_first $npm_config_last\"" } } Can this functionality also be achieved ...

Hidden text within a webpage that remains concealed until a specific link is activated

Just getting started with HTML and wanting to add animation to my project. I have a vision of an animation where clicking on an image will split open a half page of text and stuff. It should be similar to this example: ...

Why does the CSHTML button containing a JavaScript onclick function only function intermittently?

I've implemented a download button on a webpage that dynamically assigns an ID based on the number of questions posted. Below is the code for the button: <input data-bind="attr: { id: $index() }" type="button" value="Downlo ...

Deactivate toggle effect by clicking on a different link

My existing JavaScript code changes the background color of a link when it is clicked: $(".button").click(function(){ $(this).css('background-color', '#555'); }); While this functionality works, I am looking to have the color tog ...

The z-index of the React Apex Chart toolbar menu icon is too high. Is there a way to decrease it?

Is there a way to adjust the z-index value of the icons within React Apexchart's toolbar menu? Even when I position my element at a higher level, they remain on top of it. ...

Evaluating Vue.js Watchers using Jasmine

I want to write a test for a VueJS watcher method, in order to verify if it's being called. The watcher method in my component is structured like this: watch: { value: (newValue, oldValue) => { if (newValue.Status === 'Completed') ...

React-app size has grown larger post-deployment

I recently created my second app clone using React. I have noticed that after deployment, the size of the app increases. Everything looks normal on localhost:3000, but it significantly grows once deployed. Any assistance in resolving this issue would be gr ...

Canvas ctx.drawImage() function not functioning properly

I've encountered an issue while trying to display images in a canvas using my rendering function. Here is the code snippet: function populateSquareImages(){ for(var i = 0, ii = squares.length; i < ii; i++) { if(squares[i].hasImage) { ...

JavaScript is reliant on the presence of the alert() function to properly function

I have a JavaScript function that performs well, except for when I remove or comment out the alert() line. This function is designed to calculate the sum of values in up to 30 fields if they are present and contain a value. Here is an example of the HTML ...

Eliminate the static JSON by using an HTTP GET request instead

Currently diving into learning Angular and I have a question about how to replace a hardcoded JSON array with JSON retrieved using http get. Here is the link to the plunker that I am actively working on. In order to populate the dropdown menu at the top, I ...

Exploring the process of integrating absolute paths within a global SCSS file in a VueJS project

The webpack configuration in my vue.config.js file looks like this: const path = require("path"); module.exports = { pluginOptions: { 'style-resources-loader': { preProcessor: 'scss', patterns: [ "./src/style ...

Loading JavaScript in the background using Java and HtmlUnit

I am currently facing a challenge while navigating a website using HtmlUnit. This particular website adjusts certain buttons and displays or hides certain elements based on JavaScript events. For instance, there is a text input box along with a button th ...