Friday 15 June 2012

javascript - jQuery Cannot read property 'setAttribute' of undefined -



javascript - jQuery Cannot read property 'setAttribute' of undefined -

i've implemented image switch in website. worked fine broke altering wishes. code following:

if (src) { $$('#' + id + ' a.product-image img').first().setattribute("src", src);}

and i've changed to:

if (src) { $$('#' + id + ' a.product-image img').first().setattribute("src", src); $$('#' + id + ' a.popup-image img').first().setattribute("src", src); }

i added image source need changed. when run on site next error:

uncaught typeerror: cannot read property 'setattribute' of undefined

i've checked if element exists , if found adding next code script:

if('#' + id + 'popup-image' == 0){ console.log("fail"); }else{ console.log("found"); }

and returns found everytime. i've changed code this:

$$('#' + id + ' a.popup-image img').first().setattribute("src", src);

but next error:

uncaught typeerror: cannot read property 'first' of null

how prepare this?

to check if element exists should've used code:

if($$('#' + id + 'popup-image').length > 0){ console.log("found"); }else{ console.log("fail"); }

to set attribute should

if (src) { $$('#' + id + ' a.product-image img').first().attr("src", src); $$('#' + id + ' a.popup-image img').first().attr("src", src); }

javascript jquery

No comments:

Post a Comment