Working code but throwing an error?

Hi Folks, I have the below code which works but throws an error when executed? What am I missing?

export function dropdown_change(event) {
 const max = 8
 const min = 1
 if (event.target.value === '1') {
 for (let i = 2; i <= max; i++) 
        $w(`#participantbox${i}`).collapse();
    }
 if (event.target.value === '2') {
 for (let y = 2; y > min; y --)
        $w(`#participantbox${y}`).expand();
 for (let i = 3; i <= max; i++) 
        $w(`#participantbox${i}`).collapse();
    }
 if (event.target.value === '3') {
 for (let y = 3; y > min; y --)
        $w(`#participantbox${y}`).expand();
 for (let i = 4; i <= max; i++) 
        $w(`#participantbox${i}`).collapse();
    }
 if (event.target.value === '4') {
 for (let y = 4; y > min; y --)
        $w(`#participantbox${y}`).expand();
 for (let i = 5; i <= max; i++) 
        $w(`#participantbox${i}`).collapse();
    }
}

Looks to me like a simple type error in your code lines for your elements expand and collapse functions, with you using an ` instead of just a ’ or "

Try simply changing your lines from

$w(`#participantbox${i}`).collapse();

To this

$w('#participantbox${i}').collapse();

See the API reference etc for those functions…

Toggle an element’s collapsed state

if( $w("#myElement").collapsed ) {
  $w("#myElement").expand();
}
else {
  $w("#myElement").collapse();
}

He is using Template literals.

It seems to me there is a miss match on the curly bracket for value 1.
What are all of the valid id names? Is one included?

I would comment out the code and test. Then uncomment some more code to pin point which statement is failing.

@oneofusis @givemeawhisky , thanks for your response, I pasted the code to the main page away from testing and it seems to work fine with no errors… everything is the same in terms of IDs but only in the testing page it threw this error.

@oneofusis
Yeah my bad, I was just thinking of single and double quoted strings and not template strings.

  • Single quoted strings ‘literal value’

  • Double quoted strings “literal value”

  • Template strings literal value ${nonliteral}

I’ve only seen them used myself like this, so that was what was throwing me off.

$w('#someText').text = `Text is ${something}`;

Thanks for correcting me :+1:

@JDKirk @givemeawhisky , thanks for all of your help, managed to get 90 lines of code into 9

const max = 8
const min = 1

export function participants_onchange(event, $w) {
 let participantNmb = $w('#participants').value;
 for (let i = 2; i <= max; i++) 
    $w(`#participantbox${i}`).collapse();
 for (let y = participantNmb; y > min; y--)
        $w(`#participantbox${y}`).expand();
}

Thanks!