This is absurd, I feel like in kindergarten. I've already tried any combination that I could think of, and I just gave up trying.
<script type="text/javascript">
function myFunction(){
alert("test");
}
</script>
<a href="#" class="love" id="<?php echo $img_id; ?>" onclick="myFunction()">
This is a php file that is being "inserted" in a parent html, but I don't see why this would mess with javascript.