469,358 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Swap Image and Unhide Text Not Working

I have a image that I want to click and when clicked that image will be
swapped and then show a hidden div. When another image like it is
clicked it will swap image and hide that text and so on. My problem is
that when I send the Image ID's to the function I cannot find a way to
insert the dynamic names. I thought I could get the ID and concate the
string to use in the function but if I do IM = "I"+id; and stick it in
IM.SRC = ... it will not work but if I debug the value of IM it is
correct. If I manually put in IM33.SRC = ... it works but will not
work with the contacted value. What am I doing wrong? Thanks

var previd;
function swapImage(id) {
var nid = "id_"+id;
var el = document.getElementById(nid);
alert("before"+id);
var im = 'I'+id;
alert("after"+im);
if(previd!=null)
{
alert("in here"+im)
previd.style.display="none";
im.src = "images/plussign.gif"
return(false);
}
alert("Original"+im+"EL="+el);
displayType = (el.style.display == "none")?"block":"none";
el.style.display = displayType;
previd=el;
im.src = "images/minussign.gif"
return(false);
}

Apr 20 '06 #1
2 1610
Barkster said the following on 4/19/2006 10:54 PM:
I have a image that I want to click and when clicked that image will be
swapped and then show a hidden div. When another image like it is
clicked it will swap image and hide that text and so on. My problem is
that when I send the Image ID's to the function I cannot find a way to
insert the dynamic names. I thought I could get the ID and concate the
string to use in the function but if I do IM = "I"+id; and stick it in
IM.SRC = ... it will not work but if I debug the value of IM it is
correct. If I manually put in IM33.SRC = ... it works but will not
work with the contacted value. What am I doing wrong? Thanks
<snip>
im.src = "images/plussign.gif"


Mistake #1: You are relying on IE's bad habit of making the ID of an
object a global variable pointing to that object
Mistake #2: Not using the images collection to access images.

document.images[im].src = "...";
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 20 '06 #2
Thanks for clearing that up for me. Thanks

Apr 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Richard | last post: by
3 posts views Thread by Randell D. | last post: by
2 posts views Thread by Matthew | last post: by
6 posts views Thread by Chris Fink | last post: by
4 posts views Thread by bridgemanusa | last post: by
5 posts views Thread by Ed Jay | last post: by
7 posts views Thread by KiwiBrian | last post: by
2 posts views Thread by sabbas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.