Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop and return? 2

Status
Not open for further replies.

mike314

Programmer
Jun 24, 2003
143
US
Small error here. I have this function that displays the images from 1 -10 side by side, but it only displays the first when I use return. When I use document.write it works, but I need this function to return so I can use this in a div


var eachPicture;
function drawPicture(){
for(i=1;i<10;i++){
eachPicture = '<img src="'i +'.gif">';
return alpha;
}

}

how can I return each image in the loop without using document.write?

thanks!!
 
Wouldn't it actually be eachPicture += '<img src=\='i + '.gif\=>'; ??
Otherwise, it'll think you are nesting quotations marks, if I'm thinking correctly. . .
 
Return each picture to what? And what is alpha? We need more details to really answer your questions.

You also need to initialize eachPicture to concatenate the string as the examples above do. If you need to change eachPicture after it's been built, then the variable needs to be initialized inside the function each time, too.
Code:
var eachPicture[b][red]=''[/red][/b];
function drawPicture()
{
for(i=1;i<10;i++)
  {
  eachPicture += '<img src="' + i + '.gif" />';
  }
return eachPicture;
}
Lee
 
yeah need to clear some stuff up

alpha was a variable that I used in a few drafts before, forgot to change it when I posted

I now initialized eachPicture so it works now the only issue i'm having is clearing the div.

I only posted that section cause thats what was causing an issue, but here is the whole picture

<html>
<head>
<title>Untitled</title>
<script>
x = 0;
var eachPicture = '';
function drawPicture(){
x++;
for(i=1;i<10;i++){
if(i == x)
{eachPicture += '<img src="' + i +'surprise.gif">';}
else{eachPicture += '<img src="' + i +'.gif">';}
}
return eachPicture;
}


function Surprise(){
var total = drawPicture();
document.getElementById("space").innerHTML = total;
} //end of moveNext
</script>
</head>

<body>
<div id="space">
<script>
var dpic = drawPicture();
document.getElementById("space").innerHTML = dpic;
</script>
</div>


<a href="javascript:Surprise();">Next</a>

</body>
</html>


Now, thanks to your help, I just need a way to clear the div each time I click next. Is that possible?

Again, thanks!!!!!!!!!

 
if you need me to clarify stuff let me know
 
is this any better?

Code:
<html>
<head>
    <title>Untitled</title>
    <script type="text/javascript"><!--
    x = 0;
    function drawPicture(){
        x++;
        for(i=1;i<10;i++){
            if(i == x){
			    eachPicture += '<img src="' + i +'surprise.gif">';
			}else{
				eachPicture += '<img src="' + i +'.gif">';
			}
        }
        return eachPicture;
    }

    function Surprise(){    
        document.getElementById("space").innerHTML = drawPicture();
    }      //end of moveNext
    //--></script>
</head>

<body onload="Surprise();">
<div id="space"></div>

<a href="#" onclick="Surprise(); return false;">Next</a>

</body>
</html>

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Yeah it works, but is there a way to write over the div once it is write? cause it keeps the previous loops.

thanks!!!!
 
you shouldn't have your eachPicture variable globally declared. instead, you should redeclare it inside your function:

Code:
    function drawPicture(){
        x++;
        [red][b]var eachPicture = '';[/b][/red]
        for(i=1;i<10;i++){
            if(i == x){
                eachPicture += '<img src="' + i +'surprise.gif">';
            }else{
                eachPicture += '<img src="' + i +'.gif">';
            }
        }
        return eachPicture;
    }

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
thanks it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top