Dolbyover
Dolbyover

Reputation: 89

Javascript getting multiple images to appear instead of one

I am writing a function that when called prints out the same image 7 times. When calling the function I am passing it a 0 in the parameters. Here is the function:

function showCards(numcards) {
  while (numcards < 7) {
    var data = ""
    data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>"
    numcards = numcards + 1
  }
  return (data)
}

However, when this function is called, it only prints one image. How can I get it to print all seven? Any ideas? Thanks!

Upvotes: 0

Views: 41

Answers (1)

icktoofay
icktoofay

Reputation: 129001

Put the var data = "" outside of the loop. As is, it is reset on every iteration.

function showCards(numcards)
{
    var data = ""
    while (numcards < 7)
    {
        data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>"
        numcards = numcards + 1
    }
    return (data)
}

Upvotes: 6

Related Questions