Forum Moderators: open

Message Too Old, No Replies

looping through an associative array

only seems to check the first item

         

gleddy

12:05 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



Hi,

I have the following code (posted below) that is almost there but not quite.

I want each link to change the src of the iframe. But right now my code doesn't seem to want to loop through the array, but only checks the first item in this array. Not sure if my syntax is wrong?

The first link works if you want an example. The second will return negative when I think it should work.

cheers for any advice :-)

gleddy

12:05 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title>Frame Tester</title>

<script type="text/javascript">

function scoreBoard(name, location) {
this.name = name;
this.location = location;
}

var scoreURL = new Array();
scoreURL[0] = new scoreBoard("live", "http://www.news.com.au");
scoreURL[1] = new scoreBoard("golf", "http://www.webmonkey.com");
scoreURL[2] = new scoreBoard("union", "http://www.csscreator.com");

function changeFrame(id) {
for (var i in scoreURL) {
if (scoreURL[i].name == id) {
document.getElementById('score-board').src = scoreURL[i].location;
break;
} else {
alert("no");
break;
}
}
}
</script>

</head>
<body>

<ul>
<li><a href="#" onclick="changeFrame('live');">Live</a></li>
<li><a href="#" onclick="changeFrame('golf');">Cricket</a></li>
<li><a href="#" onclick="changeFrame('union');">Football</a></li>
<li><a href="#" onclick="changeFrame('live');">NBL</a></li>
<li><a href="#" onclick="changeFrame('me');">NRL</a></li>
</ul>

<iframe id="score-board" src="http://www.google.com"></iframe>

</body>
</html>

Bernard Marx

3:07 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're asking it to stop...

} else {
alert("no");
[red]break[/red];
}

Try:

function changeFrame(id) {
for (var i in scoreURL) {
if (scoreURL[i].name == id)
return document.getElementById('score-board').src
= scoreURL[i].location;
}
alert("no");
}