Javascript DOM How to - Load image by setting src properties








Question

We would like to know how to load image by setting src properties.

Answer


<!DOCTYPE html>
<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head><!--   w w  w . j a v a 2 s  .  c o m-->
<body>
  <img class="a" />
  <script>
    $(".a").attr('src',"http://www.java2s.com/style/download.png");
    console.log($(".a").length);
    </script>
</body>
</html>

The code above is rendered as follows: