Home › Forums › Backend Issues (wp-admin) › Detect image render/load within flexible content repeater › Reply To: Detect image render/load within flexible content repeater
I can’t tell you exactly what your issue is but it could be a couple of things.
The first is that in a repeater you’re likely to be getting multiple images and not just one image.
The other is that the DOM is being updated and JS has a difficult time finding elements that have just been added.
Another might be that the script is not actually running because JS added dynamically to the DOM added when the repeater row is added will not automatically be run.
Instead of adding the script using the render_field hook I would suggest adding an admin JS file as explained here https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
Then look at the ACF JS API https://www.advancedcustomfields.com/resources/javascript-api/. In particular the “ready” and “append” actions, especially the append action because this happens when a repeater is added.
I would also use jQuery, because it is loaded anyway and used by ACF.
targeting actions outside of ACF on a repeater sub field, for example an image load action, needs to start with the document object because the img object may not exist in the dom on page load.
// in your admin JS file
(function($){
$(document).on('load', '[data-key="FIELD_KEY"] img', function(e) {
var img_el = $(e.target);
console.log(img_el);
});
})(jQuery);
Hopefully something in there helps you.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.