You will need to delay the calling of the Dialog.addClickHandler() function until after the new content has been rendered, and one method you can use to do this is the :passagedisplay event.
note: you didn't include the whole of your macro's handler code, nor an example of how you call that macro, so I had to make some assumptions on what both of these things might look like.
Macro.add('doit', {
handler: function () {
/* Some assumptions. */
const item = this.args[0];
const rw = 'rw-class';
var txt = '<div>';
var id = '';
var dg = '';
/* Original code example. */
var img = 'pics/' + item.folder + item.image;
txt += '<div class="col-' + rw + '">';
txt += '<a id="' + item.id + '"> @@.itemImg;[img[' + img + ']]@@ </a>';
if(item.uses > 0) {
txt += '<<link "Uses x' + item.uses + '">>';
} else {
txt += '<<link "Count x' + item.count + '">>';
}
txt += '<<useFromInv $' + item.id + '>>';
txt += '<<goto ' + passage() + '>> <</link>>';
txt += '</div>';
id = '#' + item.id;
dg += '[img[' + img + ']]\n';
dg += item.lor;
txt += '</div>';
new Wikifier(this.output, txt);
if (dg != '') {
/* Delay the setup of the click handler. */
$(document).one(':passagedisplay', function (ev) {
Dialog.addClickHandler(id, null, function () {
Dialog.setup("Item observe");
Dialog.wiki(dg);
});
});
}
}
});
<<set $item to {
id: 'an-item',
folder: 'folder/',
image: 'image.png',
uses: 0,
count: 1,
lor: 'some lore'
}>>
<<doit $item>>
The first of the above examples used the jQuery <element>.one() function to monitor for the :passagedisplay event, and then sets up the reqired Dialog Click Handler.
warning: You may have some issues if you are calling this within a loop, or if the loop has many iterations.