Working code but throwing an error?

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();
}