Help with changing shape colors in a repeater

I’m having trouble with
I want to clearly specify content on my site that is free vs pro. For this I used a repeater with a label that has a Text element (labelText) and a Box shape (labelShape). I linked the text to the container to the corresponding Main Category ( PRO and FREE). However, I cannot get the background colors to change dynamically with the code I am using for the shape.

Code shown in this screen shot.

Working in
Wix Classic Editor

Site link

What I’m trying to do
I want the PRO color to be FF4040 and the FREE color to be 7118E1.

What I’ve tried so far
I tried to use a basic shape (vector) as well as box shape (container) and neither have worked for me. Not sure if that matters. I am limited with what I can do in the CMS editor as adding items or editing fields is greyed out.

Any suggestions or guidance would be much apprciated!

According to backgroundColor documentation, it cannot be applied to Shape objects

However, if as you say, it also didn’t work for Box elements, then there might be another problem

I don’t see any major flaws in the code that would prevent it from executing the intended functionality - so let’s rubber-duck it!

// after line 3
console.log(itemData, category)

For each item on the repeater, this should print an object containing its data, and whatever was saved within category, though those will probably show as “pro” and “free”, it never hurts to ensure that is the case

If indeed the data is correct, then it’s probably an error on Wix’s side.
A different approach could be to use the global styles and set up a CSS class for the pro shapes, then apply that class conditionally

global.css:

.proShape {
    backgroundColor: 'red'
}

page code:

$item('#labelShape').customClassList.add('proShape')

And, minor note - There is no need for 2 if statements for multiple reasons:

  1. Since category cannot be both “pro” and “free” at the same time, you do not need to check twice, use else or else if (if there’s more than 2 possibilities). As it currently stands, even if the category was “pro”, your code will still try comparing it with “free” afterwards
  2. HOWEVER - If you do only have two possibilities, then even the else wouldn’t be necessary, as you could set up the default styling to be as one of the categories, and simply change it when the item is of the other one.
    e.g. in the editor, set the shape color to #7118e1, then in code, alter it only if the category is “pro”

And, also negligeable, you do not really need to run all this within $w.onReady, you can remove the first and last lines of code

$w('#repeater1').onItemReady(($item, data) => {
    if (data.mainCategory.toLowerCase().trim() === 'pro')
        $item('#labelShape').customClassList.add('proShape')
})
1 Like

Thank you so much! mainCategorywas an object, not a string. Debugging (console.log) showed the real structure.

I appreciate your help!

I am a designer with limited coding knowledge, so this helped solve the issue.

Thank you again! :folded_hands:

1 Like