is there a way to change the onclick event (in my casee of an image) on the fly?

I have something like a flickr viewer and show a grid of images. When clicked, the image opens in a view where it is big. It's loaded dynamically only if clicked. Now I want to change the onclick of the grid-image so that instead of running the showFullImage("someid") function is just runs the, hopefully now round-trip-free, mwl-command >> mwl.setGroupTarget('#pages', '#pageFull-someid', 'show', 'hide'); <<.
I tried doing it via
document.getElementById("gridImage-someid").onclick = "mwl.setGroupTarget('#pages', '#pageFull-someid', 'show', 'hide');";

but it doesn't work when I try it on the phone or as cloud preview.