...

View Full Version : Photo/caption swap function not functioning



marilynn.fowler
06-02-2006, 09:24 PM
When a user rolls over a thumbnail the image with the ID 'placeholder' should change and the text with the ID 'caption' should change. The images are preloaded. I can't see my flaw. Here's my function:

function showProject (whichProject)
if (document.getElementById) {
document.getElementById('placeholder').src = whichProject.src;
if (whichProject.title) {
document.getElementById('caption').childNodes[0].nodeValue = whichProject.title;
}
else {
document.getElementById('caption').childNodes[0].nodeValue = whichPhoto.childNodes[0].nodeValue;
}
return false;
}
else {
return true;
}
}
\0\0
He!\0\0pف\0pف\0oad (pared down):

if (document.images) {
niche = new Image;
niche.src = 'images/more/niche.jpg';
}

Here's my line item:

<A onMouseOver="javascript:showProject('niche')" HREF="more_niches.html"
TITLE="Large kitchen niche"><IMG SRC="images/more/niche_large_t.jpg"></A>

Beagle
06-02-2006, 09:42 PM
there is no src or title property of a string.

whichProject has a value of 'niche' which is a string.

Perhaps your thinking that the string 'niche' relates to something else further up or down in your javascript or your markup. Could you paste more of your code if you need more assistance?

marilynn.fowler
06-02-2006, 10:13 PM
I actually figured it out! With my own brain!
I broke it into two functions and called them both with onMouseOver.

if (document.images) {
\0 fou\0\0hځ\0hځ\0Image();
fountain.src = 'images/more/fountain.jpg';
fountaintext = 'Fountain';

function chgImg(imgField,newImg) {
if (document.images) {
document[imgField].src= eval(newImg + ".src")
}
}

function chgCaption(whichCaption) {
if (document.getElementById) {
document.getElementById('caption').childNodes[0].nodeValue = whichCaption;
}
}

<A onMouseOver="chgImg('placeholder','fountain'); chgCaption(fountaintext); return true;"
HREF="more_fountain.html"><IMG SRC="images/more/fountain_t.jpg" NAME="fountain"></A>
\0\0
3 questions: will leaving out 'var' hurt it, and is there any way to combine it into one function that evaluates it by the NAME attribute? And why do my entries to this forum keep getting truncated/garbled

Beagle
06-02-2006, 11:24 PM
Gratz on figuring it out yourself!

Why your posts get truncated, no idea. Perhaps foreign keyset?

Anyway, the VAR keyword is used to declare variables before their use. It is good practice and will produce javascript warnings in strict mode. I would use them.

How can you make something key of an attribute of the element?

<img name="fountain" onmouseover="func(this.name);" />

How can you make it call 1 function?



function func(name)
{
func1();
func2();
}


Hope that helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum