Ruby Forum Rails Spinoffs > [object Error] troubles. Hope this saves someone some hair-pulling.

Posted by Chris Sepic (csepic)
on 13.05.2008 07:23
(Received via mailing list)
The following function worked fine in FF but not in IE - I'd get an
object Error (though I'm still not quite sure why):

function updateCartTotalPrice(element_id) {
    total_price_td = $('total_price_td');
    total_price = getPrice(total_price_td);
    this_price = getPrice($(element_id));
    updated_price = total_price - this_price;
    updated_price = updated_price.toCurrency();
    total_price_td.update(updated_price);
}

I thought for about an hour the offending code was the last line
(the .update), because the total_price_td is inside of a table. I had
heard that ie doesn't play nice with innerHTML. So I tried using
replace, and a couple other things but still no dice. Well, it turns
out the problem was actually the first line. I guess because
total_price_td is the id of an element, IE choked. I'm not sure why
though - could someone explain that? Anyway to fix it I declared
total_price_td with var like I should have in the first place. Hope
this helps someone save an hour (or 3) of frustration.
Posted by Christophe Porteneuve (tdd)
on 13.05.2008 08:29
(Received via mailing list)
Chris S a écrit :
> out the problem was actually the first line. I guess because
> total_price_td is the id of an element, IE choked. I'm not sure why
> though - could someone explain that? Anyway to fix it I declared
> total_price_td with var like I should have in the first place. Hope
> this helps someone save an hour (or 3) of frustration.

FWIW, IE will use a large set of sources when resolving
document.getElementById (which is at the heart of $()), and your
assigning the result to a same-named global variable may indeed confuse
him, although I find it a bit weird.

At any rate, you should indeed always declare your local variables as…
local, thanks to "var".  Scope pollution can be a VERY nasty thing, for
instance when function A loops over calls to function B which has a
same-name-index loop in it ;-)

--
Christophe Porteneuve aka TDD
tdd@tddsworld.com