How to create an onclick event on an image in a post?

Without specifics, in general, the page code that is output is generated by your theme code. So you would need to look into the theme’s template that is used for that page.

The template used is determined by the WordPress Theme Heirarchy , details are here: https://developer.wordpress.org/themes/basics/template-hierarchy/ ; take a look at the graphic which will help you understand which template is being used.

Then, look at that template file to find what is outputting the ‘img’ tag. That will take a bit of digging. Once you find that code, post it here to get more help.

It is also possible to use the DOM object to modify all ‘img’ tags with some Javascript, but that will affect all ‘img’ tags, which may not be what you want. Lots of tutorials on how to do that.

But without more detail of the code that is generating the ‘img’ tag, it is hard to give you a definitive answer. The above process will help you figure out what that code is.

Added

Here’s some code that will change the ‘class’ attribute in a DOM object:

function fix_img_tag() {
    $dom = new DOMDocument;
    libxml_use_internal_errors(false); // supress errors
    $dom->loadHTML($html, LIBXML_NOERROR);  // supress errors
    // img = change class to myclass (removing any other class statement)
    foreach ($dom->getElementsByTagName('img') as $node) {
        $node->setattribute('class','myclass');

        $dom->saveHtml($node) ;
    }   
    $html = $dom->saveHTML();   // saves the object (all of the html) so we can return it   
    return $html;
}

Based on that code, and the DOM Object itself, you should be able to figure out how to add the onclick element to the img tag. Note that the above code will change all img tags.

Then just figure out how to add the function to the_content() filter… hint: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

DOM is an interesting thing to know about. Lots of stuff you can do with the DOM object.

(Note that I am giving you guidance on how to learn how to do the task, and to implement it. It is good to learn things.)