What version of NodeBB and where did you place the custom javascript code?
Maybe try
<script>
$(window).on('action:ajaxify.end', function(){
if (ajaxify.data && ajaxify.data.cid && parseInt(ajaxify.data.cid, 10) === 1) {
<!-- insert html image-->
} else {
<!-- remove html image -->
}
});
</script>
Put some console.logs in there to make sure the expected code is being executed.