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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me fix my javascipt???

Status
Not open for further replies.

MrGeezer

Technical User
May 1, 2002
3
GB
Hi - I'm sure I shouldn't be posting in this forum really as I am far from being advanced enough, but this place has been my saviour on many occasion and since I can't find the info I need in a search I was wondering if any of you highly skilled folks could help me with a query.

I'm using coffeecup to design a website, and it has built in ready-made javasript that can be used. One of these scripts is an image previewer.

I copied and pasted the code as instructed into my webpage, and the result looked like this:

-----
<html>

<head>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function CoffeePreview(sel) {
document.CoffeePreview.src = &quot;&quot; + sel.options[sel.selectedIndex].value;
}
function CoffeeShow(sel) {
images = new Array();
images[1] = &quot;Acuity.jpg&quot;;
images[2] = &quot;BLBunch.jpg&quot;;
images[3] = &quot;3.jpg&quot;;
images[4] = &quot;4.jpg&quot;;
images[5] = &quot;5.jpg&quot;;
images[6] = &quot;6.jpg&quot;;
window.location.href = images[sel.selectedIndex+1];
}
// End -->
</script>
</HEAD>

</head>

<body>


<center>
<form name=previewselect>
<select name=selbox size=1 onChange=&quot;CoffeePreview(this)&quot;>
<option value=&quot;TNAcuity.jpg&quot;>Acuity
<option value=&quot;TNBLbunch,jpg&quot;>BLBunch
<option value=&quot;3-small.jpg&quot;>Image #3
<option value=&quot;4-small.jpg&quot;>Image #4
<option value=&quot;5-small.jpg&quot;>Image #5
<option value=&quot;6-small.jpg&quot;>Image #6
</select>
<p>
<img name=&quot;preview&quot; src=&quot;1-small.jpg&quot; width=150 height=113 border=1>
<p>
<input type=button value=&quot;view Image&quot; onclick=&quot;CoffeeShow(this.form.selbox)&quot;>
</form>
</center>

</body>

</html>

-----

I've got as far as renaming the first two files on each list to the names of the actual photos and left the rest so you can see what the code was and how I've modified it.

Now - the options on the listbox are as they should be (ie the first two options in the list read &quot;Acuity&quot; and &quot;BLbunch&quot; as one would expect. And clicking the &quot;View Image&quot; button which has been created does indeed produce the correct image.

But the little preview window just shows a little red X.

I'm pretty sure the line that I need to change to get this working is the one that reads

<img name=&quot;preview&quot; src=&quot;1-small.jpg&quot; width=150 height=113 border=1>

But I don't know what to change it to. If I change it to &quot;TNAcuity.jpg&quot; then it shows a preview of the first picture, but obviously won't change when I change the drop down list.

Can anyone tell me what I should insert instead of this literal file reference in order to show an image according to what the value is in the drop down box?

Hope that makes sense, and thanks in advance for any help anyone can offer.


Matt
 
First, you should make your images Array() count from zero, since that's &quot;natural&quot; for Javascript. You won't have to add 1 to the selected index that way.

Since your form isn't named form, but previewselect, the following refers to a non-existent form:

<input type=button value=&quot;view Image&quot; onclick=&quot;CoffeeShow(this.form.selbox)&quot;>

and should be instead

<input type=button value=&quot;view Image&quot; onclick=&quot;CoffeeShow(document.previewselect.selbox)&quot;>
 
Thanks for the reply - I appreciate your effort.

I modified the numbers in the brackets in the array so it went from zero, and removed the +1 counter. I also changed the line of code as you suggested. So the code now reads

***********

<html>

<head>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function CoffeePreview(sel) {
document.CoffeePreview.src = &quot;&quot; + sel.options[sel.selectedIndex].value;
}
function CoffeeShow(sel) {
images = new Array();
images[0] = &quot;Acuity.jpg&quot;;
images[1] = &quot;BLBunch.jpg&quot;;
images[2] = &quot;3.jpg&quot;;
images[3] = &quot;4.jpg&quot;;
images[4] = &quot;5.jpg&quot;;
images[5] = &quot;6.jpg&quot;;
window.location.href = images[sel.selectedIndex];
}
// End -->
</script>
</HEAD>

</head>

<body>


<center>
<form name=previewselect>
<select name=selbox size=1 onChange=&quot;CoffeePreview(this)&quot;>
<option value=&quot;TNAcuity.jpg&quot;>Acuity
<option value=&quot;TNBLbunch,jpg&quot;>BLBunch
<option value=&quot;3-small.jpg&quot;>Image #3
<option value=&quot;4-small.jpg&quot;>Image #4
<option value=&quot;5-small.jpg&quot;>Image #5
<option value=&quot;6-small.jpg&quot;>Image #6
</select>
<p>
<img name=&quot;preview&quot; src=&quot;TNAcuity.jpg&quot; width=150 height=113 border=1>
<p>
<input type=button value=&quot;view Image&quot; onclick=&quot;CoffeeShow(document.previewselect.selbox)&quot;>
</form>
</center>

</body>

</html>

*********

Still no joy :( As expected, the page loads initially with the file TNAcuity.jpg in the preview pane. I assume this is because I have explicitly named it like this:

<img name=&quot;preview&quot; src=&quot;TNAcuity.jpg&quot; width=150 height=113 border=1>

When I try to change to the second option from the list, I get the following error message from Internet Explorer

Error 'document.CoffeePreview' is null or not an object. It says the error is on line 7, which is:

document.CoffeePreview.src = &quot;&quot; + sel.options[sel.selectedIndex].value;

Any ideas? I'm sorry to be a pain in the arse about this - it's just driving me nuts because it almost works and it's ideal for my puposes.
 
There are 3 errors in this code

1) you have closed the HEAD tag twice

2) you have a comma in your option value for TNBLbunch.jpg

3) line 7 should refer to the name of your image i.e &quot;preview&quot; not CoffeePreview

line 7 should therefore read:
document.preview.src = &quot;&quot; + sel.options[sel.selectedIndex].value;


try it
 
The error message means that the object CoffeePreview is not an image. Your image name is preview, not CoffeePreview, which is your function's name. Change that and see what happens.

 
And as if by magic..... :)

Thank you very much for your help - much appreciated. I'm off to search for a good javascript tutorial so I don't have to trouble you folks again.

Again - many thanks.

Matt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top