tags:

views:

54

answers:

2

My image roll over works... But only one way.

function heartOver(id)
{

    if(document.getElementById('heart' + id).src == 'images/heart.png');
    {
     parent.document.getElementById('heart' + id).src = 'images/unheart.png';
    }

    if(document.getElementById('heart' + id).src == 'images/unheart.png');
    {
     parent.document.getElementById('heart' + id).src = 'images/heart.png';
    } 

}


<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;">

If i comment out either of the IF statements the other will work but they wont work together...

Note: Tried this with a else if no luck.

Figured it out... Duh: i have if ( ) ; No ; after if...

+5  A: 

Put an else between them - otherwise when the first if evaluates as true, it will cause the second if to be evaluated as true also!

Here's a simplified example which also checks the assumption that the img element even exists:

var img=document.getElementById('heart' + id);
if (img)
{
    if(img.src == 'images/heart.png')
    {
        img.src = 'images/unheart.png';
    }
    else if(img.src == 'images/unheart.png')
    {
        img.src = 'images/heart.png';
    }
} 
else
{
     alert("Ooooh! No heart"+id+" element found!");
}
Paul Dixon
I tried that. It didn't change things..
ian
then you did something wrong - it's definitely the *major* problem
annakata
in your original you refer to parent.document inside the if block, was that intentional? I've simplified my example to eliminate that possible mistake.
Paul Dixon
Yea when I use a else if. Both clauses do nothing...
ian
Then alert(img.src) to see what the value actually is!
Paul Dixon
Yes the parent.document is intentional but not necessary. Tried it without parent. still no luck.
ian
and alert(id), maybe it's not what you're expecting, and thus you don't actually find an img element at all.
Paul Dixon
Haha no its... my stupid mistake. ; after the if
ian
bah! never mind :)
Paul Dixon
I gave you the correct answer anyways. Thanks.
ian
A: 

Is this correct?

<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;">

Try with this:

<img src="images/heart.png" id="heart<?php echo $row['id']; ?>" alt="Love!" width="16" height="16" border="0" onmouseover="heartOver(<?php echo $row['id']; ?>)" onmouseout="heartOver(<?php echo $row['id']; ?>)">