Miva Merchant Development by Scot's Scripts

JQUERY AJAX: Detecting Clicks on Ajax Content

Miva Knowledge Base
JQUERY AJAX: Detecting Clicks on Ajax Content

WARNING:

This information is offered "AS IS" for internal reference only. Use at your own risk.
Does AI actually understand your Miva Merchant store? Our smart JSON-LD schema generator makes sure it does. Contact us to get started. (more info)

JQUERY AJAX: Detecting Clicks on Ajax Content

Scot Ranney • July 09, 2024


THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED;
THE AUTHOR DISCLAIMS ALL LIABILITY FOR ANY DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR STORE DISRUPTIONS ARISING FROM ITS USE.


Detecting clicks on AJAX injected content must reference the parent element, not just the ID or CLASS that is being clicked.

In this case, the AJAX content is injected as HTML into the parent-element div. There are two lines of example AJAX content.

<div id="parent-element">
<span class="some-class" data-something="This is something">Ajax content, Click Me 1</span>
<span class="some-class" data-something="This is something else">Ajax content, Click Me 2</span>
</div>

<script>
$("#parent-element").on('click', '.some-class', function(){
		var $el = $(this);	
  	alert('something:' + $el.data('something'));
});
</script>

In this case we are detecting a click on a class called some-class but it could also be an ID #some-class

The alert will display whatever is in the data-something attribute.


https://www.scotsscripts.com/mvblog/jquery-ajax-detecting-clicks-on-ajax-content.html

mvkb_ajax mvkb_jquery