Josh Spires
Josh Spires

Reputation: 5

Replacing commas with spaces for classes in Shopify

I am using product tags as a way to filter what users see depending on if they are commercial or consumer customers. I have managed to get the tags into the class of each product displayed in a collection using the following code. The {{itemTags}} is being used to pass the value over into the HTML.

itemHtml = itemHtml.replace(/{{itemTags}}/g, data.tags);

<div class="{{itemTags}}">

The only issue is that all of the tags are displayed in a string with no spaces and commas in between each one. Resulting in the following class being added as an example.

class="accessories,Consumer,DJI Air 2S,live,pre-order,preorder"

Is it possible to remove the commas and add spaces in between each tag?

Upvotes: 0

Views: 900

Answers (1)

Charles C.
Charles C.

Reputation: 3923

use the replace tag in liquid

itemHtml = itemHtml.replace(/{{itemTags | replace: "," , " " }}/g, data.tags);

Upvotes: 1

Related Questions