First change to (since you are using jquery, note that this should be added after including jquery lib):
<script type='text/javascript'>
function imgLoaded(img){
img.parent().addClass('loaded');
};
</script>
Second, you should wrap this code in document ready:
$(function() {
$("img").attr({onload:"imgLoaded(this)"});
});
This way you will trigger the functionality once all nodes are added to dom tree. If you used that outside document ready then it wouldn't have any effect since $("img") would be empty upon executing that code (having in mind you put that in the < head > section)
You can also put
<script>
$("img").attr({onload:"imgLoaded(this)"});
</script>
before closing < /body > tag (to ensure entire page was rendered ).