jQuery HTML Element How to - Get the source of the image








Question

We would like to know how to get the source of the image.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){<!--from w ww  .  ja va2 s.c  o m-->
    console.log( $('img').attr('src') );
    console.log( $('img')[0].src );
});//]]>  
</script>
</head>
<body>
  <img src="http://www.java2s.com/style/download.png" alt="Arnold">
</body>
</html>

The code above is rendered as follows: