# Javascript question: using the image button

**URL:** <https://community.unix.com/t/javascript-question-using-the-image-button/324427>\
**Category:** Programming\
**Created:** [February 16, 2013, 1:49pm UTC](https://community.unix.com/t/javascript-question-using-the-image-button/324427 "2013-02-16T13:49:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![figaro](https://community.unix.com/letter_avatar/figaro/32/6_449c533f586939b6e5f246632a46421f.png) [@figaro](https://community.unix.com/u/figaro)\
**Post date:** [February 16, 2013, 1:49pm UTC](https://community.unix.com/t/javascript-question-using-the-image-button/324427/1 "2013-02-16T13:49:07Z")

</div>

I have javascript that I want to use to build up a dynamic array: the user clicks on one or more images and when clicking the Show button, the list of image names is displayed that the user clicked.

```nohighlight
<script type="text/javascript">

var images = [];
images[1] = "http://www.unix.com/images/image01.jpg";
images[2] = "http://www.unix.com/images/image02.jpg";
images[3] = "http://www.unix.com/images/image03.jpg";
images[4] = "http://www.unix.com/images/image04.jpg";

for (var i in images)
   document.write('<input name="myImage' + i + '" id="myImage' + i + '" type="image" src=' + images + ' alt="Add me..." onclick="add();" />');

var arr = [];

function add() {
   var inp = document.getElementById('myImage1');
   arr.push('myImage1');
   inp.value = '';
}

function show() {
   document.getElementById('container').innerHTML = arr;
}

</script>

<input type="button" onclick="show();" value="show" />
<div id="container"></div>

```

The code has a function add() which is obviously wrong, but has at least a sample value to show what happens when an image is clicked and subsequently the Show button.

How do I correct the code to only show the names of the images that the user clicked?

SOLVED:

```nohighlight
<script type="text/javascript">

var images = [];
images[1] = "images/image01.jpg";
images[2] = "images/image02.jpg";
images[3] = "images/image03.jpg";
images[4] = "images/image04.jpg";

for (var i in images)
   document.write('<input name="myImage' + i + '" id="myImage' + i + '" type="image" src=' + images + ' alt="Add me..." onclick="arr.push(\'myImage' + i + '\')" />');

var arr = [];

function show() {
   document.getElementById('container').innerHTML = arr;
}

</script>

<input type="button" onclick="show();" value="show" />
<div id="container"></div>

```
