Jump to content

Recommended Posts

Basically the idea is to create a function in existing code, that would display an image caption below the image in a div with the id photoCaption encapsed in <p> tags.

 

I'm new to javascript, so Im truly an idiot when it comes to googleing anything like this.

 

Here is what I have tried so far (this is non working code):

 

function addCaption() {
    if (thumbnails[currentImageIndex].className.match(/description:=(.+)=/)) {
addCaption = document.getElementById('photoCaption');
var descriptionInfo = document.createElement('p');
var addCaption = document.getElementById('photoCaption').firstChild;
       addCaption.data = '%CAPTION%' + RegExp.$1;

    }
}

 

The expected output would be (Caption: is set in a variable CAPTION, and the caption itsself is embedded in the class of the thumbnail image as description:=".$image['caption']."= )

<p>Caption: This is what the caption of the current image says</p>

 

Now obviously the code above doesn't work, which is why I am here. What am I doing wrong.

 

Link to comment
https://forums.phpfreaks.com/topic/73482-solved-need-a-crash-course-in-java/
Share on other sites

ok this code works:

function addCaption(thumbIndex) {
    if (thumbnails[currentImageIndex].className.match(/description:=(.+)=/)) {
var itemCaption = document.getElementById('photoCaption').firstChild;
var dataCaption = document.createElement('p');
var caption = document.createTextNode(CAPTION + RegExp.$1);
dataCaption.innerHTML = itemCaption.appendChild(caption)
    }
}

 

Now my problem lies in the fact that every time I click a new thumbnail, it appends the data to the existing <p> tag that was created when i first clicked the thumbnail:

 

<div id="photoCaption">
<p>Caption: SunsetCaption: SunsetCaption: Blue hills with a blue sky backdropCaption: Winter in AlaskaCaption: Blue hills with a blue sky backdrop</p>

 

How do I get it to update the caption instead of appending it to the end?

Figured this one out on my own, thanks for the help though.

 

What I did was add an attribute to the html tag called caption, and added the caption to that attribute, then:

 

function addCaption(thumbIndex) {
    	var itemCaption = document.getElementById('photoCaption').firstChild;
itemCaption.data = thumbnails[thumbIndex].getAttribute('caption');
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.