473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Javascript breaks when images added

This javascript is meant for learning language vocabulary, specifically
colours on this example. However, when I use images in the mtWord array,
it breaks when checking for correct answers. It works fine with text in
those spaces.

Direct links to the pages online:
http://www.lajzar.co.uk/en/colours.html
http://www.lajzar.co.uk/en/animals.html

How can I fix this so the functions work with images in that array? It
works fine with a table and appropriately coloured backgrounds, but I
cant get gold and silver that way.

Or nose at teh code pasted below...

---snip---

var mtWord = new Array (
"<IMG SRC='blue.png'>",
"<IMG SRC='green.png'>",
"<IMG SRC='orange.png'>",
"<IMG SRC='pink.png'>",
"<IMG SRC='white.png'>",
"<IMG SRC='black.png'>",
"<IMG SRC='yellow.png'>",
"<IMG SRC='red.png'>",
"<IMG SRC='gold.png'>",
"<IMG SRC='silver.png'>",
"<IMG SRC='brown.png'>",
"<IMG SRC='purple.png'>"
);

var enWord = new Array(
"blue",
"green",
"orange",
"pink",
"white",
"black",
"yellow",
"red",
"gold",
"silver",
"brown",
"purple"
);

var mtOrder = new Array(12); // the number of items in the arrays above
var enOrder = new Array(12); // the number of items in the arrays above
var picNum = new Array(36); // the number of problems to display x 3

var maxProb = 6; // the number of problems to display
var Riable = 12; // the number of items in the arrays above

/************************************************** *******************
mixes up n elements of nArray starting at element startAt
************************************************** ********************/
function Shuffle( nArray, startAt, n ) {
var i, j, swap;
for (i = startAt; i < startAt + n; i++) {
j = startAt + Math.round(Math.random() * (n - 1));
swap = nArray[i];
nArray[i] = nArray[j];
nArray[j] = swap;
}
}

function showProblems() {
var i, j, n, prob, pnum;
var d, str, picname;

for (i=0; i<Riable; i++) { mtOrder[i] = i; }
Shuffle( mtOrder, 0, Riable );
for (i=0; i<maxProb; i++) { enOrder[i] = mtOrder[i]; }
Shuffle( enOrder, 0, maxProb );

pnum = 0;
d = top.document;
d.open();
for (prob=0; prob<maxProb; prob++) {
picNum[prob] = pnum++;
d.write("<TR>\n<TD><IMG NAME='pic", prob, "'
SRC='null.gif'></TD>\n");
d.write("<TD><INPUT TYPE='text' NAME='in", prob, "'
SIZE=1></TD>\n");
d.write("<TD>", mtWord[mtOrder[prob]], "</TD>\n");
d.write("<TD WIDTH=20></TD>\n");
d.write("<TD>",prob+1,". ", enWord[enOrder[prob]],
"</TD>\n</TR>\n\n");
}
d.close();
}

function checkAnswers() {
var i;
var ok;
var estr;

for (i=0; i<maxProb; i++) {
ok = false;
n = parseInt( top.document.qform.elements[i].value );
if ((! isNaN(n)) && n >= 1 && n <= maxProb ) {
if (enOrder[n-1] == mtOrder[i]) { ok = true; }
}
if (ok) { estr = "yay.gif"; }
else { estr = "nay.gif"; }
top.document.images[picNum[i]].src = estr;
}
}

function doNull() { }
// -->
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME="qform">
<TABLE border=1>
<SCRIPT LANGUAGE="JavaScript">
<!--
showProblems()
// -->
</SCRIPT>
</TABLE>
</FORM>

<HR>
<P>Type the number of the correct English word before each English word,
then click the
<A HREF="javascript:doNull()" onclick="checkAnswers();">here</A> to see
if you're right.</P>

---end snip---
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
2 1647
> top.document.images[picNum[i]].src = estr;

I got a little tied in a knot following the code but I would say this line
is the problem. picNum[i] seems to be an integer so when you try to change
the images you are acting on the first six (or whatever) images on the page.
This will be OK when they are the first six images on the page but with the
images being used for the colours too they aren't any more, they are more
like 0, 2, 4... etc and if you add an image higher up for decoration that
will break it too.

I believe you want to be giving an argument of the form "pic1", "pic2" etc
to access those images by their name. So that would make it something like
top.document.images['pic'+picNum[i]].src I think. As I say I got a bit tied
up!

Jul 20 '05 #2

"Graham J" <in***************@orangebucket.co.uk> wrote in message
news:bn************@ID-203032.news.uni-berlin.de...
top.document.images[picNum[i]].src = estr;
I got a little tied in a knot following the code but I would say this

line is the problem. picNum[i] seems to be an integer so when you try to change the images you are acting on the first six (or whatever) images on the page. This will be OK when they are the first six images on the page but with the images being used for the colours too they aren't any more, they are more like 0, 2, 4... etc and if you add an image higher up for decoration that will break it too.

I believe you want to be giving an argument of the form "pic1", "pic2" etc to access those images by their name. So that would make it something like top.document.images['pic'+picNum[i]].src I think. As I say I got a bit tied up!


Thanks! That fixed the problem perfectly!
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Duderonomoy | last post by:
Hello, I am querying the JavaScript community to learn if others are having problems with Safari and JavaScript arrays: myImages = ; then referenced like this:
3
by: annon | last post by:
I've noticed that some problems come up frequently that are of importance in writing web pages, because they're pretty fundamental points. For general reference, here are some collected...
2
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can...
1
by: pstrand2000 | last post by:
My page uses a horizontal navigation bar that requires a JavaScript file to force IE to do hovers. It worked great in both Fire Fox and IE until some Flash code was added. This is how the Flash...
12
by: tim | last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus in firefox. On my sub3 menus i have more than one line of text in some places. firefox does not recognise that there is more...
20
by: alice | last post by:
I'm doing some text swapping with javascript, got it working fine, but I would like the line to have line breaks and being a beginner, I don't even know if this is possible. So I have a line like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.