Sorry vbkris, as I said, the html code is not under my control. I write the JS function to serve a general purpose and work on images identified by their id's, no matter how they are embedded in their html page.
The suggestion by vongrunt seems to be the right solution. Thanks, I'll try it.