Change button color when clicked on Dynamic Page

I have a dynamic page that is filtering itens, example when I select or click in the button1 that show me a category of products in my gallery.
I want to everytime that button1 is clicked it changes color, that way the user can see which button/category is selected.

I have the same issue. Anyone???

+1, please help )

Can you visualize it ā€¦
What is the element that you want itā€™s color to change?

You can duplicate whatever that item is and change the design of it and choose to show and hide by the clickevents you have.

Iā€™ve done it with 2 different buttons, one grey enabled upon load, one with blue disabled upon load.
for every pair of buttons i wrote this code

// This function makes the ā€œbuttonā€ be disabled (grey)
export function button28_click(event, $w) {
$w(ā€œ#button28ā€).hide();
$w(ā€œ#button34ā€).show();
}
// This function makes the ā€œbuttonā€ be enabled (blue)
export function button34_click(event, $w) {
$w(ā€œ#button34ā€).hide();
$w(ā€œ#button28ā€).show();

1 Like

You can enable and disable the button on itā€™s own
just use enable or disable instead showing and hiding 2 different buttons.
Just relate it on another event.

$w("#yourbuttonid").enable(); //enables it 
$w("#yourbuttonid").disable(); // disables it.
1 Like

I tried it. But if you disable the button, you cannot click it againā€¦ And I need my users to enable/disable it as much as they want

Oh so itā€™s for enabling disabling some other thingā€¦
But i still didnā€™t get what you are trying to achieve here

Showing another button to state an element is disabled
when it is the same element makes no sense at all :slight_smile:

Iā€™m sure there is a better way to do what youā€™re trying to achieve
Can you put a picture of it or a link if itā€™s live.

Imagine 3 buttons: one for Adults, one for Children and one for Youth. I want the users to choose which is relevant by pressing those buttons. Once a button is pressed, it changes its color. When itā€™s not pressed, itā€™s grey and when itā€™s pressed itā€™s another colorā€¦ say blueā€¦
So itā€™s easy to visualize which buttons were pressed. Kind of like a check box but a different design for it.

I agree that there should be a better way to do it. I looked and looked and couldnā€™t find it. But what I did works fine for now.
Cheers

1 Like

That makes sense now.
I should sleep more :slight_smile:

Hi Mert ,
They want to achieve this thing:


This site created with Wix Code: https://www.unidraft.com/projects-en
Thread on the Forum: https://www.wix.com/code/home/forum/show-off-your-work/unidraft-website

Dear Andrew Kov,

yes, thatā€™s exactly what i need!
do you have any ideas, how to do that?

BR,

I solved it by choosing one of the menus that have design options for different designs for ā€˜regularā€™, ā€˜hoverā€™ and ā€˜clickedā€™.

Just need to add: I had simply hidden text (as menus are auto-generated) with white overlay that could be clicked and the color lit up from design options from menus when clicked, - but itā€™s a cumbersome solution (Iā€™m only using it for a prototype for customer research, however I do want to test if that helps to navigate the search in that exact design format - so hoping Wix adds a features of buttons with design options for ā€˜regularā€™, ā€˜hoverā€™ AND ā€˜clickedā€™ as for menus!)

1 Like

Another work-around is an Anchor Menu, also not ideal, but with some work you can give it another color when clicked.
(Iā€™m fully aware none of these are good enough, but if you want to TEST the feature on customers, they work.)

I solved this by attaching a function to the button click.
The function will find the element id and use that to get the element; a button.

Now we have the button element, we can mutate some properties as needed; see the docs of what you can change: https://www.wix.com/code/reference/$w.Button.html
In this case, we want to change the background and text colours. The style property holds these.

You need to have some logic of what state the button is in to determine how to affect the style.

Hereā€™s what I wrote:

export function button_click(event) {
 let button = $w("#" + event.target.id)
 let borderColour = button.style.borderColor
 let colour = button.style.color

 if (isClicked(borderColour)) {
        setButtonColours(button, BLACK) 
    } else {
        setButtonColours(button, GREEN)
    }
}

function isClicked(borderColour) {
 return borderColour === GREEN
}

function setButtonColours(button, colour) {
    button.style.borderColor = colour;
    button.style.color = colour
}
1 Like

I think you could use the button events onMouseIn and onMouseOut to replicate hovering.

This code really helped me thank you anatarad!

Here is how you can Change button color when clicked on Dynamic Page and create a tabbed activity using a M ultistate box
Firstly select the multistate box by clicking the Add+ button on Wix editor the select interactive and then select multi-state box

Then create 2 buttons for 2 states (You can also create more than 2 states the process remains the same) and write the below code in the code section

$w.onReady(function () {
 // TODO: write your page related code here...
    $w("#button1").onClick(() => {
        $w('#statebox8').changeState("State1");
        $w("#button1").disable();
        $w("#button2").enable();
    });
    $w("#button2").onClick(() => {
        $w('#statebox8').changeState("State2");
        $w("#button2").disable();
        $w("#button1").enable();
    });
});

Lastly, uncheck the enabled button (from the property panel) of the first button which will be shown in grey (Disabled) as shown in the image below so as to show the default state ie first state when the page is loaded.

1 Like

nice, thank you.