0

I have a JSON from PHP assoc array:

Array(
    [0] = > Array(
        [topicid] = > 17
        [entry] = > number 12 this one
        [belongdate] = > 2017 - 06 - 12
        [edittime] = > 2012 - 06 - 18 05: 22: 21
        [User_ID] = > 1
    )
    [1] = > Array(
        [topicid] = > 9
        [entry] = > yeah 11 now
        [belongdate] = > 2017 - 06 - 12
        [edittime] = > 2012 - 06 - 18 05: 22: 02
        [User_ID] = > 1
    )
)

I don't give you JSON straightly is because I don't know how to print JSON properly.

I need to create parent div and multiple children elements:

        <div name='entrydiv' class='entrydiv'>          
            <h3 name='topich3' class='topich3'>
                entryjson[i]['topicid']
            </h3>
            <p name='entryp' class='entryp'>
                entryjson[i]['entry']
            </p>
            <a name='belongdatea' class='belongdatea' style='display: none;'>
                entryjson[i]['belongdate']
            </a>
            <a name='lastedittimea' class='lastedittimea'>
                entryjson[i]['edittime']
            </a>
        </div>

Note the contents are variable from JSON. I have checked out this post. But the .append method does't seem to support passing variable to the content. And the .appendTo method does not allow child element(?). I appreciate your help very mush. Thanks.

Community
  • 1
  • 1
Ivan Wang
  • 8,306
  • 14
  • 44
  • 56
  • *"But the .append method does't seem to support passing variable to the content."* What did you try exactly? – Felix Kling Jun 17 '12 at 21:57
  • Well, I tried this (maybe I just didn't do it right) `topicid=entryjson[i]['topicid']; $("

    topicid

    ").appendTo('#bodydiv');`
    – Ivan Wang Jun 17 '12 at 22:03
  • 1
    Use string concatenation. For example `var str = "

    " + entryjson[i]['entry']" + "

    ";`
    – Victor Jun 17 '12 at 22:11
  • @B7ackAnge7z Woow, I works perfectly. So it was all about escaping "". Thank you soooo much. – Ivan Wang Jun 17 '12 at 22:34
  • No, it's not about escaping quotes, it's about building the string correctly. How should JavaScript know that inside your string, `topicid` is a referring to a variable (and why not e.g. `entrydiv`)? How can it know you want to substitute it with its value? It can't. – Felix Kling Jun 17 '12 at 23:51

2 Answers2

1

Here's one example. To create this DOM structure:

<div name='entrydiv' class='entrydiv'> 
    <h3 name='topich3' class='topich3'>
       entryjson[i]['topicid']
    </h3>
</div>

You could use this plain javascript code:

var mainDiv = document.createElement("div");
mainDiv.name = mainDiv.className = "entrydiv";
document.body.appendChild(mainDiv);

var item = document.createElement("h3");
item.name = item.className = "topich3";
item.innerHTML = entryjson[i]['topicid'];
mainDiv.appendChild(item);

You can add code for the other parts and then add a loop to iterate through your JSON to repeat.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

If you would like to create the following structure:

<div class="yourClassInHTML">
...
<div>
<img src="thePath"></img>
</div>
...
</div>

then .js code would be:

var element1 = document.createElement("div");
var element2 = document.createElement("img");
img.src = "thePath";
element1.appendChild(element2);
document.getElementsByClassName("yourClassInHTML")[0].appendChild(element1);
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53