0

Im currently trying to set up a loop that returns exif data from an image using piexifjs.

Up to this point the code provided by the developer of piexif is working correct in my Vue.js environment.

The loop which is used to display the exif-data inside the console looks like this:

for (let ifd in exifObj) {
    if (ifd == "thumbnail") {
        continue;
    }
    console.log("-" + ifd);
    for (let tag in exifObj[ifd]) {
        console.log("  " + piexif.TAGS[ifd][tag]["name"] + ":" + exifObj[ifd][tag]);
    }
}

Works fine, but if I try to edit it to return HTML and display the exif information on my Site it just returns one line of information. Here's the code:

for (let ifd in exifObj) {
    if (ifd === "thumbnail") {
        continue;
    }
    const   exifInfo = $(".modal-exif"),
            exifInfoDetail = $(".modal-exif-detail");
    exifInfo.html("» " + ifd);
    for (let tag in exifObj[ifd]) {
        exifInfoDetail.html("<li>" + piexif.TAGS[ifd][tag]["name"] + ":" + exifObj[ifd][tag] + "</li>");
    }
}

I guess my loop is overwriting itself where it returns just one value, which, I guess, is fine inside the console since but not inside HTML?

Anyone got any Idea hwo to change the loop to return one line for each entry?

Thank you :)

2
  • It isn't the for loop that isn't working. You just misunderstood how jQuery.fn.html() works. Read the relevant documentation article for more information. Commented Feb 9, 2019 at 15:56
  • .html() overrides. So instead you should append to a string and do a final call to .html() when you have everything in the string. Commented Feb 9, 2019 at 15:56

1 Answer 1

3

In the console.log case, each iteration is writing to the console, whereas in the $(selector).html() case, you are overwriting the html content per iteration. The jquery html function states:

Set the HTML contents of each element in the set of matched elements.

That means that each iteration, the html content of $(".modal-exif-detail") is set to one single "<li>" + piexif.TAGS[ifd][tag]["name"] + ":" + exifObj[ifd][tag] + "</li>".

You need to use something like append if you want to continually add to a node, or build up the html string and only execute one html after the loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.