How to visually deactivate a flat button ( <input type="button"> ) programmatically with JavaScript

I am facing an issue with my buttons. I have one regular button and another flat button created using input elements. After every click, I want to disable the buttons for 5 seconds. The disable function is working properly for the normal button, but for the flat button, I need it to visually appear disabled by being dimmed or frozen and also disable hover effects.

function enable(id) {
    document.getElementById(id).disabled = false;
}

function disable(id) {
    alert(id+" is disabled for 5 seconds.")
    document.getElementById(id).disabled = true;
    setTimeout(function () {
        enable(id);
    }, 5000);

}
#buttonflat {
    border: 0;
    background: lightblue;
    box-shadow: none;
    border-radius: 0px;
    color: red;
}

#buttonflat:hover {
    background-color: blue;
    color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
    <h1>DISABLE FLAT BUTTON</h1>
    <br>
    <button id="button" onClick=disable(id)> GOL_101_019_010 </button>
    <br><br>    
    <input id="buttonflat" type="button" value="GOL_101_019_010" onClick="disable(id)" />
    <br><br>




</body>

</html>

Answer №1

To adjust the brightness, you can utilize the filter property and create a disabled effect by tweaking the brightness level.

filter: brightness(80%);

function enable(id) {
  document.getElementById(id).disabled = false;
}

function disable(id) {
  alert(id + " is disabled for 5 seconds.")
  document.getElementById(id).disabled = true;
  setTimeout(function() {
    enable(id);
  }, 5000);

}
#buttonflat {
  border: 0;
  background: lightblue;
  box-shadow: none;
  border-radius: 0px;
  color: red;
}

#buttonflat:hover {
  background-color: blue;
  color: white;
}

#buttonflat:disabled {
  user-select: none;
  filter: brightness(80%);
}
<h1>DISABLE FLAT BUTTON</h1>
<br>
<button id="button" onClick=disable(id)> GOL_101_019_010 </button>
<br><br>
<input id="buttonflat" type="button" value="GOL_101_019_010" onClick="disable(id)" />
<br><br>

You can customize the background-color to lightblue and the text color to red to give the appearance of it being unhoverable.

You can also modify the cursor style using:

cursor: not-allowed;

function enable(id) {
  document.getElementById(id).disabled = false;
}

function disable(id) {
  alert(id + " is disabled for 5 seconds.")
  document.getElementById(id).disabled = true;
  setTimeout(function() {
    enable(id);
  }, 5000);

}
#buttonflat {
  border: 0;
  background: lightblue;
  box-shadow: none;
  border-radius: 0px;
  color: red;
}

#buttonflat:hover {
  background-color: blue;
  color: white;
}

#buttonflat:disabled {
  background-color: lightblue;
  color: red;
  user-select: none;
  filter: brightness(80%);
  cursor: not-allowed;
}
<h1>DISABLE FLAT BUTTON</h1>
<br>
<button id="button" onClick=disable(id)> GOL_101_019_010 </button>
<br><br>
<input id="buttonflat" type="button" value="GOL_101_019_010" onClick="disable(id)" />
<br><br>

Answer №2

To simplify things, you can apply styling to the disabled attribute like this:

[disabled]{
  opacity:0.5;
}

This CSS snippet will set the opacity of any element with the disabled attribute to 0.5.

function enable(id) {
    document.getElementById(id).disabled = false;
}

function disable(id) {
    alert(id+" is disabled for 5 seconds.")
    document.getElementById(id).disabled = true;
    setTimeout(function () {
        enable(id);
    }, 5000);

}
#buttonflat {
    border: 0;
    background: lightblue;
    box-shadow: none;
    border-radius: 0px;
    color: red;
}

#buttonflat:hover {
    background-color: blue;
    color: white;
}

[disabled]{
  opacity:0.5;
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
    <h1>DISABLE FLAT BUTTON</h1>
    <br>
    <button id="button" onClick=disable(id)> GOL_101_019_010 </button>
    <br><br>
    <input id="buttonflat" type="button" value="GOL_101_019_010" onClick="disable(id)" />
    <br><br>
</body>

</html>

Answer №3

To implement the feature of disabling and enabling elements on a webpage, you can take advantage of CSS properties like opacity and pointerEvents. Below are sample functions that showcase how this can be achieved:

function enableElement(id) {
    document.getElementById(id).disabled = false;
    document.getElementById(id).style.pointerEvents = "auto"; 
    document.getElementById(id).style.opacity = "1";
}

function disableElement(id) {
    alert(id+" has been disabled for 5 seconds.");
    document.getElementById(id).style.pointerEvents = "none"; 
    document.getElementById(id).style.opacity = ".5";
    document.getElementById(id).disabled = true;
    
    setTimeout(function () {
        enableElement(id);
    }, 5000);
}

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

Guide to centering a Material UI Table on a webpage

Is it possible to center the <Table> within a fixed width <div>, and have a scrollbar appear when the browser is resized, while maintaining the fixed width of the <Table>? Thanks in advance. This is my current setup: <div> ...

How to deal with an empty $_FILES array when there is file data present in $_POST

After reviewing multiple solutions, I noticed that they all utilize jQuery's .ajax() method. However, I have developed a more concise vanilla JS approach which has been quite successful for me. function ajax(options){ var settings = { met ...

The synergy between HTML and JavaScript: A beginner's guide to frontend coding

I have primarily focused on server-side development of enterprise applications (Java EE, Spring framework). However, I am now exploring client-side technologies for better understanding (not necessarily to become an expert). I have studied HTML and CSS th ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

Styling a modal popup using session storage in CSS

I recently created a modal popup using CSS by following a helpful tutorial that can be found at this link: Now, I am curious about how to store the value entered in a textbox as session data. I came across some code in another tutorial that demonstrated h ...

What are the steps to resolve issues with URL Encoding?

Here is my website link: forumdisplay.php?462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA I suspect that the url is URL Encoded. I am using vbulletin 4.1.12 How can I resolve this issue? Replace 462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA with f=462 ...

The error encountered is related to the MongooseServerSelectionError that occurs in

I have been working on setting up my first mongo-db application to connect to the server. However, I am encountering a specific error during this process: const express = require('express'); const mongoose = require('mongoose'); const ...

Show small information box when hovering over custom toggle button

I have developed a unique custom toggle switch feature When I hover my mouse over the switch, it should display a tooltip message as YES if the switch is in the ON position and NO if it's in the OFF position. Is there a way to implement this functio ...

Trouble with attaching a click event to a jQuery datepicker

I am attempting to attach a click event to .ui-state-default after a jQuery datepicker has been displayed (I do not have access to the html generating the datepicker so I cannot utilize any datepicker-specific events) and it functions properly using $.bind ...

Update the ngView content on the fly

My project requires dynamic routes to be generated when specific URLs are requested in order to customize the view and display corresponding data uniformly. While adding manual routes with the same templateUrl and controller would have made this task simpl ...

utilizing geographical coordinates in a separate function

I have a program that enables users to access the locations of communication cabinets. I am attempting to utilize html5 to transmit latitude and longitude information to a php script (geo.php) in order to update the database record with map coordinates. Ho ...

Why does trying to package a Windows app on OSX prompt a request for Wine installation?

For several months, I have been successfully utilizing Electron on macOS (10.11.6) to create and package both OSX and Windows applications. My current setup includes electron v1.7.3 and "electron-packager" "^8.5.2", all of which have not been updated in a ...

Insert a horizontal line within the text

I am struggling to get the horizontal lines to work on my traffic light dashboard view for one of my website pages. Despite making multiple adjustments, it seems like I just can't get them to display correctly. Here is an example of what I want: View ...

Creating a basic bar graph using d3.js with an array of input data

In my dataset, I have an array of form data that includes items like 'Male', 'Female', 'Prefer Not To Say'. I want to create a simple bar chart generator using D3.js that can display the percentages or counts of each item in t ...

Every time Fetch() is called in Node.js, a fresh Express session is established

Here is a snippet of code from a webshop server that includes two APIs: ./login for logging in and ./products to display products. The products will only be displayed after a successful login. The server uses TypeScript with Node.js and Express, along wit ...

Could Knockout's value binding potentially lead to a script injection vulnerability?

My understanding is that knockout effectively deals with this scenario, but I just want to double-check. Is it correct to assume that when using the value binding with a form control such as input/textarea, there is no risk of a script injection attack? O ...

Could implementing a click/keydown listener on each cell in a large React datagrid with thousands of cells impact performance?

Years ago, before the advent of React, I mastered linking events to tables by attaching the listener to the <tbody> and extracting the true source of the event from the event target. This method allowed for a single listener for the entire table, as ...

Extract information from the table using $_POST

I possess a datatable within a form. Inside that table, there is a column designated for setting a price. Upon submitting the form on the same page, the $_POST data fails to populate. Consequently, I am in need of inserting it into the MySQL db post-submit ...

Ways to identify the visible elements on a webpage using JavaScript

I'm working on a nextjs app , I am looking to dynamically update the active button in the navbar based on which section is currently visible on the page. The child elements of the page are structured like this: <div id="section1" > < ...

Data update using AJAX and codeigniter was unsuccessful

How can I update my data using Codeigniter and AJAX for submitting the response? Below is the View section of my code: <form id="form_update" action="<?php echo base_url() ?>admin/update_derap_info" method="POST" role="form"> <textare ...