How can I enable a button to be clicked depending on a checkbox if it is marked or unmarked?
Platform: Wix Studio
I have an homepage with a button1 to proceed to a new page. I want that button to be clickable only if visitors mark a checkbox1
I started with this code and the button is not clickable on load, but when I mark the checkbox does not change:
$w.onReady(() => {
$w(‘#checkbox1 ’).checked=false
if ($w(‘#checkbox1 ’).checked==true) {
$w(‘#button1 ’).enable();
} else $w(‘#button1 ’).disable();})
I read around about it in old threads like this from which I took part of the code:
Is my code correct? It seems not working for me. Could someone help me out with issue.
I am trying to enable “Button 1” when “checkbox 1” is marked otherwise disable. My website link below and code is in Join Flock Now 1 dynamic page . https://flocktravel1.wixsite.com/-site/
import wixData from ‘wix-data’;
$w.onReady(() => {
$w(‘#checkbox1’).onChange(nextButtonEnableDisable);
});
function nextButtonEnableDisable() {
if (($w(‘#checkbox1’).checked
$w(‘#button1’).enable();
} else $w(‘#butt…
But when it says “link the code to checkbox” and about functions/onChange it’s not clear to me, also it refers to Wix Editor.
Thank you so much for support
Additional information:
[Include any other pertinent details or information that might be helpful for people to know when trying to answer your question.]
You’re running the check only when the page loads, and right after specifically setting it to false
Setting it to false is redundant, you can remove that line
The check should happen in its own function, fire that function when the field updates
1 Like
Thank you Dean, took off the false.
How can i fire the function when the fields update? I don’t know how to code that.
Here’s the documentation for the proper event trigger
https://dev.wix.com/docs/velo/api-reference/$w/checkbox/on-change
$w(‘#checkbox1’).onChange(() => {
if ($w(‘#checkbox1’).checked) $w(‘#button1’).enable()
else $w(‘#button1’).disable()
})
1 Like
Thank you so much, that worked!