tags:

views:

64

answers:

2
function swapImg(imgId) {
    var image = document.getElementById(imgId);
    if (image.getAttribute("class") == "unselected") {
     image.setAttribute("src", "img/frownie.gif");
     console.log(image);
    } 
}

if i look at what's outputted to console, the src of the image is correct. but it's not changing in my browser. what's up? (ie, the image stays the same even though the source url is changing...)

A: 

I would write your function this way, this should also take care of your error:

function swapImg(imgId) {
    var image = document.getElementById(imgId);
    if (image.className == "unselected") {
        image.src = 'img/frownie.gif';
        console.log(image);
    }   
}
Doug Neiner
that's how i had it the first time, and it didn't work.
hatorade
just tried it again - doesn't work :( this is strange!
hatorade
I am curious what browser and version number you are using. I assume Firefox, but what version? The two errors you have had tonight are abnormal behavior. I wonder if your browser isn't corrupted somehow.
Doug Neiner
the first one fixed :) i don't think i remembered to FTP my solution using apostrophes around the param i passed. if it helps i'm coding for an iphone app so i'm testing it with firebug in FF but everything is messed up since it's coded for an iphone
hatorade
Ah! Your `console.log` statements are generating errors on the iphone possibly. You might want to write a `if(typeof(console) == 'undefined') console = {log:function(){}};` or something like that in the page.
Doug Neiner
that didn't fix it
hatorade
Hi I think console is for Firebug, it is the Firebug consoleso console.log() just prints something out.Mabye use JQuery to walk around this issue? is this acceptable?
Michael Mao
You want to use the console if it *isn't* undefined: if(typeof(console) != 'undefined')
Guffa
@Guffa, I was creating a dummy object and function stub. Of course I only want to define it if `console` **IS** undefined.
Doug Neiner
i just want to use JS to swap my image!!! i don't need firebug!!
hatorade
@hatorade Firebug doesn't exist on the iPhone, so if you have `console.log()` statements littered through your code they will throw an **ERROR** on the iPhone. That is why this has relevance.
Doug Neiner
FWIW you should definitely stick to the DOM-HTML properties like `className` and `src` as here. Don't ever use `getAttribute`/​`setAttribute` for HTML: there are many IE bugs with it and it's generally less readable.
bobince
A: 

ugh i'm dumb. i accidentally wrote id instead of class in my code so there are multiple images with the same id, which meant i found the first one and swapped it and not the others. thanks for trying :(

hatorade
I knew something was wrong. I even built a complete test here and was just about to post it: http://pixelgraphics.s3.amazonaws.com/stackoverflow/1837134/index.htmlProving it works :) ... Also, you really should use `.className` and `.src` instead of the function calls. Direct property access is faster than calling a function.
Doug Neiner