1

I have some javascript which runs a timer that animates something on the website.

It all works perfectly but I want to get an image to change when the animation is run, It uses jquery:

if(options.auto && dir=="next" && !clicked)
{
    timeout = setTimeout(function(){
if (document.images['bullet1'].src == "img/bulletwhite.png")
{
        document.images['bullet1'].src = "img/bullet.png";
        document.images['bullet2'].src = "img/bulletwhite.png";
}
animate("next",false);
                    },diff*options.speed+options.pause);
                }

options.auto means that its automatically cycling, dir is the direction of the motion and clicked is whether or not the user clicked it.

Is there something wrong with my syntax here? I ran firebug with it, and it doesn't throw any errors, it simply won't work. Any advice would help!

I should mention that the src of bullet1 starts at bulletwhite.png and then I was hoping for it to change to bullet.png and have bullet2 change to bulletwhite.png.

4
  • You have a misplaced semicolon on the 2nd line. Probably not the answer you're looking for, but just wanted to let you know. Commented Jun 7, 2010 at 20:28
  • I guess that the last line should not be with a semicolon as well Commented Jun 7, 2010 at 20:29
  • Hard to say with all those conditions which we don't know. Are you sure it reaches the lines you want? Commented Jun 7, 2010 at 20:31
  • the lines are green in firebug so I believe even the if statement is triggering as true, I also tried trying to grab the images with getElementById to no avail Commented Jun 7, 2010 at 20:34

2 Answers 2

1
document.images['bullet1'].src == "img/bulletwhite.png"

Are you sure this condition is ever met?

Usually the imageElement.src property holds absolute/resolved version of what is in the src attribute. (This also applies to href attribute/property.)

<img src="/images/img1.png">

...

document.images[0].src => "http://127.0.0.1/images/img1.png"
Sign up to request clarification or add additional context in comments.

2 Comments

hmm interesting, would this also apply to getElementById?
It does not have anything to do with how you obtain reference to the image element, it is still the same element. (In other words: yes.)
0

According to the code you've posted there's a strange semicolon on second line, but maybe it's just a typo.

Why don't you try a JQuery attr() call instead of using document.images array?

$('img[name="bullet1"]').attr('src', 'img/bullet.png');

3 Comments

What is the supposed advantage of using jQuery here over native and w3c-compliant document.images? It is also quite likely that the jQuery approach is a bit slower: parsing the selector expression (+ there's probably some more stuff hidden behind '$(...)') vs. accessing collection property via key
In other words I was right - you were comparing property to attribute, and it has also nothing to do with jQuery. Also name attribute is deprecated. :)
@Pete: argh! 'image' is really bad! Sorry... I'll correct it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.