I need code to disable a button for one day after clicking that isn’t used to submit a form and is only linked to another page?
from Wix AI Assistant
To disable a button for one day after it has been clicked, you can use the disable() function from the Button module of $w. This function disables the button and sets its enabled property to false.
However, to keep the button disabled for one day, you need to store the time of the click in a persistent storage like Wix's database or local storage in the user's browser. For this example, I'll use local storage.
Here's how you can do it:
First, you need to import the wix-storage module to use local storage.
Insert
Copy
import { local } from 'wix-storage';
Then, in the onClick event handler of the button, you can disable the button and store the current time in local storage.
Insert
Copy
$w("#myButton").onClick( (event) => {
$w("#myButton").disable();
local.setItem("lastClicked", new Date().getTime().toString());
});
Finally, in the $w.onReady function, you can check if the button was clicked within the last day and if so, keep it disabled.
Insert
Copy
$w.onReady(function () {
const lastClicked = local.getItem("lastClicked");
if (lastClicked) {
const timeElapsed = new Date().getTime() - Number(lastClicked);
const oneDay = 24 * 60 * 60 * 1000; // time in milliseconds
if (timeElapsed < oneDay) {
$w("#myButton").disable();
} else {
$w("#myButton").enable();
}
}
});
This code will disable the button for one day after it has been clicked. After one day, the button will be enabled again. Please replace "myButton" with the actual ID of your button.
Using a connected database to each button would be even a better approach → control over all button-states.