Please help with code toggle

Hello,
does anyone know, wha my code doesn’t work? I am trying to learn wix velo and I am following tutorials, but I keep getting errors. I am trying to do simple collapsible text on toggle.

Thanks for help.

$w.onReady(function () {

$w(‘#rdmr’).onClick() => {
toggle($w(‘#addtxt’)‘));
// $w(’#addtxt’).expand();
});

});

function .toggle(txt) {
const isCollapse = txt.collapsed;
if (txt === “#addtxt”) {
if(isCollapse){
txt.expand();
}

} else {
txt.collapse();

}
}

Hi there!

This is on the right track. There are some slight issues with the JavaScript syntax you used but you correctly identified that you need to use collapse() and expand().

Here is a working implementation of a button element that handles a collapsable text element:

$w.onReady(function () {
    $w('#buttonElement').onClick(() => {
        toggle($w('#textElement'));
    });
});

function toggle(txt) {
    const isCollapse = txt.collapsed;
    if (isCollapse) {
        txt.expand();
    } else {
        txt.collapse();
    }
}
1 Like