Javascript Element How to - Create an array of images as links








Question

We would like to know how to create an array of images as links.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from   w w  w  .ja va 2 s  . co  m-->
var images = [
    {image: 'http://www.java2s.com/style/download.png'},
    {image: 'http://www.java2s.com/style/download.png'},
];
images.forEach(function(item){
    item.image = '<a href="#"><img src="'+item.image+'"/></a>';
    document.write(item.image+'<br />');
});

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: