/    Sign up×
Community /Pin to ProfileBookmark

Onclick With Image and Description w/html

Hello,
I am looking for a little assistance. Using the following code, I can click and change images with a brief “title” description…This is great and all but I would like to be able to add HTML to format the image description. When I insert tags it just displays the tags…any recommendations?

Thank you!

[SIZE=2]<script type=”text/javascript” language=”javascript”>
function showPic (whichpic) {
if (document.getElementById) {
document.getElementById(‘placeholder’).src = whichpic.href;
if (whichpic.title) {
document.getElementById(‘desc’).childNodes[0].nodeValue = whichpic.title;
} else {
document.getElementById(‘desc’).childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
}
</script>

<!–BEGIN LARGE IMAGE DISPLAY–>
<table><TR><TD>
<div id=”desc”>Click A Thumbnail</div>
<img id=”placeholder” src=”place_holder.image.jpg” alt=”” /><BR>
</TD>
<td>
</TD>

<!–THUMBNAIL IMAGES BEGIN–>
<td><a onclick=”return showPic(this)” href=”img1.jpg” title=”Photo1 desc”><img src=”img1_t.jpg” border=0><BR></a>

<a onclick=”return showPic(this)” href=”img2.jpg” title=”Img2 Desc”><img src=”img2_t.jpg” border=0><BR></a>
[SIZE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzNov 11.2006 — You would need to create element nodes via document.createElement().

Since this is simple and does not involve forms, you can use the non-standard (though cross-browser compatible for simple things) "innerHTML" property instead of "childNodes[0].nodeValue".
Copy linkTweet thisAlerts:
@koacamperauthorNov 11.2006 — Hi Kravvitz,

Sorry, I'm extremely new at this...can you direct me to an example of this in action?

Thank you.
Copy linkTweet thisAlerts:
@KravvitzNov 11.2006 — Try this. (Short explanation below.)
&lt;script type="text/javascript"&gt;
function showPic(whichpic) {
if(!document.getElementById) return true;
document.getElementById('placeholder').src = whichpic.href;
document.getElementById('desc').innerHTML = (whichpic.title)?whichpic.title:whichpic.innerHTML;
return false;
}
&lt;/script&gt;

The language attribute of <script> is deprecated don't use it. Use the type attribute by itself.

I replaced the inner if/else with the [url=http://en.wikipedia.org/wiki/%3F:]ternary operator (read this)[/url].

I replaced "childNodes[0].nodeValue" with "innerHTML" so all child nodes of the element will be copied to the other element instead of just grabbing the nodeValue of the first child.

I inverted the check for document.getElementById to make it a little shorter by removing the need for an else block.
Copy linkTweet thisAlerts:
@koacamperauthorNov 12.2006 — Thank you so much! That did the trick! I sincerely appreciate the lesson!
×

Success!

Help @koacamper 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.18,
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,
)...