put all in document ready handler and see if helps:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
var prev = '';
$('#zzz img').click(function(){
if (prev != '')
$('#img_' + prev).css('border', '1px solid #eee');
prev = $(this).attr('id').replace('img_', '');
$(this).css('border', '1px solid #535666');
$('#imgid').val(prev);
});
});
</script>
Hoping that you are having jQuery plugin loaded before this script.
solved not working js script