I am trying to give a unique HTML ID to a heading inside a collection list item. It does not have any CMS content tied to it. But obviously, as it is part of the HTML collection list item, it gets duplicated with the every new collection list item, also duplicating it’s ID and making the ID non-unique.
It doesnt have to be a particular ID, it just has to be a unique one for each heading in each collection list item.
Using Javascript my approach so far was the following:
Use a script inside an embedded HTML element to find the innerText of another heading inside the collection list item that gets its content from the CMS and is therefore unique content.
--
The is problem that : $(this).find("#referenceHeadingId ").innerText; results in undefined.
And I tried and tested a lot but can’t seem to find the solution.
Is there maybe a better way to generate a unique ID for each heading in each collection list item?
Or is there a better way to access the innerText of an element?
collection page - No way to create unique-ids by JS (Each collection page is with the same code = same id if you work by JS).
To generate ID number (start from X) for html list of p/li/divs or any idea like this - its very simple (If you want i will add codepen) - but what the idea behind this id? (Why not using regular cms field? like number-field)
You have the right idea of dynamically setting the ID using the heading text. The issue is that you cannot use inline jQuery in embed code blocks, and have to do it in the page footer code.
Hi @Elias, @Siton_Systems, @samliew and @asgerkrause, I’m having the same issue. I need every collection item on a page to have a unique id so we can track it with Google Tag Manager. What would be a good solution to create unique id’s for every item, any id-eas?
@Coding_isnt_my_thing The problem with your example is that the .idtext paragraph element is not a direct child of your .w-dyn-item. yours is set out like this .w-dyn-item>section>div>div>.idtext but it must be .w-dyn-item>.idtext for it to work
@Coding_isnt_my_thing your very welcome and yes you just have to move the .idtext element so its just inside the .w-dyn-item and not inside anything else
I didn’t see this as a recommendation, but here’s the approach I take.
Since you’re already working in an HTML Embed, you have full control over your element’s ID.
In your example…
<h5 id="changeMe" ...
You can insert the Slug field and add static text adjacent to it.
Slugs are guaranteed to be unique for each item within a collection, because they are used to form the item’s Collection page URL. You can think of them like a primary key for each of your collection items.
For example…
This also makes your ID’s predictable for code use if you need to reference them- which is typically what ID’s are for.