0

I'm trying to convert tags into tags in a Shopify store in content that's dynamically loaded through a 3rd party app, meaning the HTML is added after the page finishes loading. Before I do the conversion, the tags already have 'click' event listeners added to them through the 3rd party script.

My current code looks like this:

var widgetHTML = $(".cbb-frequently-bought-selector-list").html();
widgetHTML = widgetHTML.replace(/<a /g, '<span ').replace(/<\/a>/g, '</span>');
$('.cbb-frequently-bought-selector-list').html(widgetHTML);

After this code executes, the original 'click' event listeners are gone.

Is there a way to temporarily save a reference to all existing 'click' event listeners on all tags, then execute the above code, then add the 'click' event listeners back onto the new tags that replaced the tags?

Ben Chan
  • 240
  • 3
  • 10

1 Answers1

0

Use jQuery's event delegation like this:

$(document).on('click','your_element_selector', function() {
   // your code    
});
guramidev
  • 2,228
  • 1
  • 15
  • 19