1

This might be such a simple fix, but I think I've been stuck on this issue for way to long. Can someone spare a pair of eyes and see where I'm messing up?

I'm pulling data from a json file and trying to append the right value with the right container. Right now it's placing all the values in the first div container.

var metaDataArray = resultSet.searchResult[i].metadatatoshow;

        for (var xx = 0; xx < metaDataArray.length; xx++) {
            var metaDataContainer = "<div id='itemMetaDataContainer_"+xx+"'>Meta Data:" + metaDataArray[xx].name + "-</div>"

            for (var xxx = 0; xxx < metaDataArray[xx].meta_values.length; xxx++) {

                $('#itemMetaDataContainer_'+xx).append(metaDataArray[xx].meta_values[xxx]);

            }

            itemDetailsDiv.append(metaDataContainer);
        }
1
  • #itemMetaDataContainer_XX is still a string when you try to append some content to it. If it's not, you've multiplied ids within the document. Commented Nov 6, 2014 at 19:31

1 Answer 1

2

you are trying to select a container before you append it to the details div. try appending the data values array to your container the following way:

var metaDataArray = resultSet.searchResult[i].metadatatoshow;

for (var xx = 0; xx < metaDataArray.length; xx++) {
    var metaDataContainer = $("<div id='itemMetaDataContainer_" + xx + "'>Meta Data:" + metaDataArray[xx].name + "-</div>");
    for (var xxx = 0; xxx < metaDataArray[xx].meta_values.length; xxx++) {
        metaDataContainer.append(metaDataArray[xx].meta_values[xxx]);
    }
    itemDetailsDiv.append(metaDataContainer);
}

UPDATE:

a jQuery object holds the plain html element in index [0] (assuming its not an array).
to get the html out of metaDataContainer you can find it in metaDataContainer[0]

therefore, in order to append the jQuery object metaDataContainer to the plain html element itemDetailsDiv, you need to do the following:

itemDetailsDiv.innerHtml + = metaDataContainer[0];

UPDATE 2:

Here is a plain JavaScript solution:

var metaDataArray = resultSet.searchResult[i].metadatatoshow;

for (var xx = 0; xx < metaDataArray.length; xx++) {
    //initializing the html string with the container opening tag and the description:
    var metaDataContainer = "<div id='itemMetaDataContainer_" + xx + "'>Meta Data:" + metaDataArray[xx].name;

    //looping through the values and adding them to the html string:
    for (var xxx = 0; xxx < metaDataArray[xx].meta_values.length; xxx++) {
        //create a span for each value and add it to the html string
        metaDataContainer+="<span>" + metaDataArray[xx].meta_values[xxx] + "</span>";
        //add a line break just for aesthetics, you can change to suit your formatting requirements
        metaDataContainer+="<br/>";
    }
    //finish by adding a closing tag for the container
    metaDataContainer+="-</div>"

    //append the new html string to the details div
    itemDetailsDiv.innerHtml +=metaDataContainer;
}
Sign up to request clarification or add additional context in comments.

11 Comments

Makes sense! But now I'm getting Uncaught TypeError: undefined is not a function in console
probably because of your itemDetailsDiv, is it a jQuery object? because if its not, append() wont work on it
It's not a jQuery object. Is there a way to do this without using jQuery?
you already used jQuery as soon as you used the $("") selector. if you would like to append it without jQuery, you will need to extract the html object from the jQuery object and add it to the inner Html of the details div, i will update my answer.
Do I replace itemDetailsDiv.append(metaDataContainer) with your updated code? Or place it in the nested loop?
|

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.