/    Sign up×
Community /Pin to ProfileBookmark

change image onclick

I have a page that list tiems a user can add to a shipping list (cookie based). In my javascript code, I have this, which actually changes text on my page from Add to Shopping List to Remove From shopping List:

[code]
function Item(i,e) {
if(document.getElementById(e).firstChild.firstChild.data==’Add to shopping list’) {
document.getElementById(e).firstChild.firstChild.data=’Remove from shopping list’;
document.getElementById(e).firstChild.setAttribute(“title”,”Remove from shopping list”);
addItem(i);
return true;
} else {
document.getElementById(e).firstChild.firstChild.data=’Add to shopping list’;
document.getElementById(e).firstChild.setAttribute(“title”,”Add to shopping list”);
removeItem(i);
return true;
}
}
[/code]

I need to change this so that it actually changes an image now, instead of text. How would I do this? I tried replacing the text string with the path to my images, but that did not do anything, like this;

[code]
function Item(i,e) {
alert(“called script”);
if(document.getElementById(e).firstChild.firstChild.data==’/images/btn_add_shopping_list.png’) {
alert(“adding to shoppinglist”);
document.getElementById(e).firstChild.firstChild.data=’/images/btn_remove_shopping_list.png’;
document.getElementById(e).firstChild.setAttribute(“title”,”Remove from shopping list”);
addItem(i);
return true;
} else {
document.getElementById(e).firstChild.firstChild.data=’Add to shopping list’;
document.getElementById(e).firstChild.setAttribute(“title”,”Add to shopping list”);
removeItem(i);
return true;
}
}
[/code]

I get the first alert, but not the second.

Thanks in advance!!!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 12.2006 — If you change it to an image it would not be this anymore:

firstChild.data

it would be this:

firstChild.src
Copy linkTweet thisAlerts:
@jrthor2authorMay 12.2006 — Thanks. Now that gives me the entire path, including http://www.sitename.com/dir/dir/images/image.jpg. How can I have this so that when I check for the image name, and then replace the image, I don't need to specify the entire url? So when I check firstChild.src, I don need this:

if(document.getElementById(e).firstChild.src=='http://localhost:9081/images/btn_add_shopping_list.png')

I could just use this:

if(document.getElementById(e).firstChild.src=='/images/btn_add_shopping_list.png')

Thanks!!!
Copy linkTweet thisAlerts:
@KorMay 12.2006 — To avoid errors (IE and Moz count different the childNodes, if a gap is inluded, better use getElementsByTagName()

document.getElementById(e).getElementsByTagName('img')[0].src


I could just use this:

if(document.getElementById(e).firstChild.src=='/images/btn_add_shopping_list.png')
[/quote]

No, you can not use relative url, as src (and href as well) returns the absolute address.

You may use a RegExp to test
<i>
</i>var re=//images/btn_add_shopping_list.png/;
if(re.test(document.getElementById(e).getElementsByTagName('img')[0].src)){
......
}
Copy linkTweet thisAlerts:
@phpnoviceMay 12.2006 — I could just use this:

if(document.getElementById(e).firstChild.src=='/images/btn_add_shopping_list.png')[/QUOTE]

For assignment (setting) purposes, you can use a relative url, for comparing purposes (getting), I use [b]indexOf()[/b] -- for example:
var obj = document.getElementById(e).firstChild;
if(obj.src.indexOf('btn_add_shopping_list.png') &lt; 0)

As mentioned, though, just make sure there's no gap or newline between your image and your link.
Copy linkTweet thisAlerts:
@jrthor2authorSep 11.2006 — Any reason that sometimes in IE, when I click my Add to Shopping List image, it does not swithc to the Remove from Shopping List image using the below code? Firefox seems to work fine 100% of the time.
<i>
</i>function Item(i,e) {
//var re=//images/btn_add_shopping_list.png/;
var obj = document.getElementById(e).firstChild;
if(obj.src.indexOf('btn_add_shopping_list.png') &gt; 0) {
obj.src='/www.riteaid.com/w-content/images/btn_remove_shopping_list.png';
document.getElementById(e).setAttribute("title","Remove from shopping list");
document.getElementById(e).setAttribute("alt","Remove from shopping list");
//obj.setAttribute("title","Remove from shopping list");
addItem(i);
return true;
} else {
obj.src='/www.riteaid.com/w-content/images/btn_add_shopping_list.png';
document.getElementById(e).setAttribute("title","Add to shopping list");
document.getElementById(e).setAttribute("alt","Add to shopping list");
//obj.setAttribute("title","Add to shopping list");
removeItem(i);
return true;
}
}
Copy linkTweet thisAlerts:
@jrthor2authorSep 12.2006 — can anyone help? I am kind of in a time crunch.

Thanks
Copy linkTweet thisAlerts:
@jrthor2authorSep 14.2006 — Can anyone please help me???

Thanks
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...