Help with adding and calling a unique ID for CTA in a repeater
SOLVE
Hi everyone,
So, I have a repeater module and inside of it I added a cta that has a modal option. But the pop-up from the modal is not unique and all of the cta's are displaying the content from the latest repeater and not the one for their own.
I added the ability to add an ID to the button: {% if item.single_cta_group.choose_type_of_cta == "modal_cta" %} <div class="cta-grp" > <div class="row-fluid-wrapper"> <div class="row-fluid"> <div data-id="{{ item.single_cta_group.add_modal_id_here ~ loop.index }}" class="modal-cta left-col"> <a class="cta_button">{{ item.single_cta_group.add_modal_cta_name_here }}</a> </div> </div> </div> </div> {% endif %}
I'm calling back to the ID in the popup: {% if item.cta_group.single_cta_group.choose_type_of_cta == "modal_cta" %} <div id="{{ item.single_cta_group.add_modal_id_here ~ loop.index }}" class="custom-model-main popup"> <div class="custom-model-inner"> <div class="close-button">×</div> <div class="custom-model-wrap"> <div class="pop-up-content-wrap"> <p>{{ item.cta_group.single_cta_group.add_popup_text }}</p> </div> </div>
Help with adding and calling a unique ID for CTA in a repeater
SOLVE
You're so close! Give this a try:
$(".modal-cta").on("click", function() {
let target = $(this).attr("data-id");
$(`#${target}`).addClass("model-open");
});
Instead of relying on HubL variables in the jQuery, you can use your data-id attribute to target the ID of the modal you want to open. Shout out to @Jaycee_Lewis because they were on the right track with their response above.
Help with adding and calling a unique ID for CTA in a repeater
SOLVE
You're so close! Give this a try:
$(".modal-cta").on("click", function() {
let target = $(this).attr("data-id");
$(`#${target}`).addClass("model-open");
});
Instead of relying on HubL variables in the jQuery, you can use your data-id attribute to target the ID of the modal you want to open. Shout out to @Jaycee_Lewis because they were on the right track with their response above.
Help with adding and calling a unique ID for CTA in a repeater
SOLVE
Hi Jayce, I haven't resolved it.
when I add it to the jquery, I do something like: $( "#{{ item.single_cta_group.add_modal_id_here ~ loop.index }}".modal-cta" ).on('click', function(event) {
and tried a few other methods but I just end up breaking the button. The issue has to be here but I'm thinking that my implementation is wrong ... (obviously) ... 😅.
Help with adding and calling a unique ID for CTA in a repeater
SOLVE
Hey, @dsanchezRW👋 Were you able to get this resolved?
If not, have you tried to modify your jQuery to read the data-id attribute of the clicked CTA and use it to open the matching modal by ID? Making sure each CTA and modal pair has a unique data-id and id respectively. And adjusting the jQuery click event to target and display the modal based on these IDs.
Best,
Jaycee
PS — Thanks for including your code sample and a link. That's always helpful 🙌
Help with adding and calling a unique ID for CTA in a repeater
SOLVE
So I got a step closer, my ID's weren't showing up in the html because my id path was wrong. I updated it to be id="{{ item.cta_group.single_cta_group.add_modal_id_here }}" If you refresh or clear the chache then you'll see the difference.
As for the jquery (which is not my strong suit), I have it looking like this: $(document).ready( function() { $( "#{{ item.cta_group.single_cta_group.add_modal_id_here }}" ).on('click', function(event) { // var tab_no = $(this).attr('data-tab'); // console.log(tab_no); $(".custom-model-main.popup").addClass('model-open'); $("body").addClass('overflow-hidden'); return false; }); $(".close-button").click(function(){ $(".custom-model-main").removeClass('model-open'); $("body").removeClass('overflow-hidden'); }); });
But now I have a warning that reads: HubL in your CSS or JavaScript won’t be parsed, except for the module_asset_url function