0

I am trying to create a table of 6 images, 2 rows and 3 columns large. I have created an array for all of them in the head of the program. Should it be in the body? The images within the table will later need to be clickable, directing to the same page (ie. google.com). I don't know how to put the images individually into the table that I have created.

<head>
  <script>
    var img = new Array(6);
    img[0] = new (400,200)
    img[0].src"image1.jpg"
  </script
</head>

... and so on for all the rest of the images

<body>
    <table border ="0">
    <tr>
    <td>document.write(img[0]) </td>
    </tr>
    </table>
</body>

... and so on for the rest of the images

2
  • Have edited it to find a bug (notice the missing closing tag in the script), but still don't know what was asked here. ) Commented Jun 25, 2012 at 22:43
  • I'm asking how do i place the images from my array into the table that i have created. Commented Jun 25, 2012 at 22:50

2 Answers 2

4

You appear to be neglecting DRY - Don't Repeat Yourself. You are repeating code for every image you want to create. Instead, try this:

<script type="text/javascript">
    (function() {
        var images = [
            "image1.jpg",
            "photo.jpg",
            "graphic.png",
            "animation.gif"
        ], columns = 3,
        l = images.length, i,
        table = document.createElement('table'),
        tbody = table.appendChild(document.createElement('tbody')),
        tr, td, img;
        for( i=0; i<l; i++) {
            if( i % columns == 0) tr = tbody.appendChild(document.createElement('tr'));
            tr.appendChild(document.createElement('td'))
              .appendChild(document.createElement('img'))
              .src = images[i];
        }
        document.body.appendChild(table);
    })();
</script>

This will basically insert the table with all the images right where the script is. To add more images, just add more URLs to the array. To change how many are shown on each row, just change the columns variable. This code is more complicated than your original code, but on the other hand it is much more scaleable and easier to modify.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Way to throw a little of everything in there :) This is the approach I would take in production but it could be a little over this person's head. It would be nice if you explained your use of a self-executing closure, the dom appending, and the dom element creation for the op.
+1 for the same reasons. But I won't be surprised if what's the OP now thinks about this is something like "???!!!???" )
It doesn't allow for the case where the number of images isn't evenly divisible by the number of rows. Perhaps not important to the OP. The chaining isn't pretty and not easily maintainable.
0

This is one way you could do it:

<td><img id="theImage" /></td>
<script>
 document.getElementById("theImage").src = img[0].src;
</script>

2 Comments

what is "theImage"? a link or the name ie. img[0]
@iamtheuser - "theImage" is just the id of the image tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.