1

I'm wondering what might be the simplest approach to dynamically changing the contents of a "caption" div to reflect the info corresponding to a specific thumbnail/link in an image gallery.

To be more specific, if you visit this link-- which shows off the awesome Seadragon zoom script btw-- I would like to have a small caption under the image that changes (text) content when a user clicks the different links above; perhaps pulling text from an alt or title attribute and placing in an empty div?

In my case, I'll be using thumbnails instead of text links, so upon clicking these images the user will both initiate the "switchTo" Seadragon event and fill the empty div with corresponding content.

thanks for any guidance here.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
nickpish
  • 839
  • 4
  • 24
  • 43

1 Answers1

2

If you have the id of the div, the simplest way would be to use innerHTML:

document.getElementById(id).innerHTML = "text";

This is fine for simpler cases, like replacing all of the text in a div. If you want to do something more complicated, like having other html elements inside the div, this method is not the best choice.

If you want to add an html element to the div tag, then you should avoid innerHTML. If you have, say, a variable img that is an img tag, then use this to add it to the div:

document.getElementById(id).appendChild(img);

If you want to do anything more complicated than this, you should probably consider using a nice framework like jQuery--it might be too much for something like this, but if you plan to do anything else, then a framework would be worth using.

If you want to do this with jQuery, it can be done using some of the following functions:

var div = $("#id");// jQuery uses CSS selectors to get elements.
div.append("New content");// Puts the new content at the end.
div.empty();// Gets rid of everything inside the div.
div.append(element);// You can also append elements.

// For example, you can create and append an image to the div:
var img = $("<img>");
img.attr("src", "images/something.png");
div.append(img);
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • 1
    This will work fine for most things otherwise I believe there is a document.createElement function or use a framework like jQuery. – James Khoury Apr 14 '11 at 23:35
  • @James - you are completely right. I originally assumed nickpish just wanted to put some text in a div. I've broadened my answer a bit. Maybe it's more useful now. – Tikhon Jelvis Apr 14 '11 at 23:48
  • thanks for the responses-- how specifically would I use the jQuery approach? – nickpish Apr 14 '11 at 23:53
  • The short answer: var div = $("#id") gives you the div. div.append(stuff) appends the new content. div.empty() empties the div if you want. Be sure to include some version of jQuery with a script tag. – Tikhon Jelvis Apr 15 '11 at 00:03
  • thanks to the suggestions above, I have a working test gallery going here: [link](http://www.nickpish.com/zoom/index.html) and I'm wondering if any of you guys would check out my modest jQuery scripting effort and see if it's kosher? I'm sure there are more sophisticated ways to code this function, but it seems to work well in all the browsers I've tried. I'm also curious how I might go about inserting more rich content-- say, with images as well, not just text-- into the "caption" container; perhaps by wrapping in a div initially set to display:none and placing it in the container via jQuery? – nickpish Apr 21 '11 at 06:56
  • Look at my very last code snippet: in it I create an image, set its src and append it to a div. If that div is what is currently holding the caption, then the image will appear there. – Tikhon Jelvis Apr 21 '11 at 07:32
  • Your code looks fine; however, you should consider putting it in a separate .js file, especially if it is to grow soon. – Tikhon Jelvis Apr 21 '11 at 07:36
  • awesome, thanks Tikhon; also, something interesting I noticed while testing this-- in IE, if I placed the seadragon code first and then the jQuery, the former wouldn't seem to load-- is there a logical reason for this or just IE bad behavior? – nickpish Apr 21 '11 at 07:41
  • I can't think of any good reason for this, and don't have IE handy to take a look at it. IE being what it is, if it is the only one doing this, chances are it's at fault... – Tikhon Jelvis Apr 21 '11 at 07:57
  • ok thanks... seems I can always rely on some mystifying IE behavior no matter what the case. I was also thinking it'd be cool to initially display a static jpeg on the right, then allow the user to "turn on" the deepzoom functionality, thus replacing the regular jpeg and loading seadragon; not sure how involved that'd be though... – nickpish Apr 21 '11 at 08:06