0

This is my HTML code.

 <div id="ulgallery">
    <ul class="row">
      <li> some item1 </li>
      <li> some item1 </li>
      <li> some item1</li>
    </ul>
    <ul class="row">
      <li> some item2 </li>
      <li> some item2 </li>
      <li> some item2 </li>
    </ul>
  </div>

I have a background ajax program which fetches more

 <ul> ...</ul> <ul> ...... </ul>

data as string. I have a problem in appending this data after the last element.

the final code should look like:

 <div id="ulgallery">
    <ul class="row">
      <li> some item1 </li>
      <li> some item1 </li>
      <li> some item1</li>
    </ul>
    <ul class="row">
      <li> some item2 </li>
      <li> some item2 </li>
      <li> some item2 </li>
    </ul>
   <ul class="row">
      <li> some item3 </li>
      <li> some item3 </li>
      <li> some item3 </li>
    </ul>
   <ul class="row">
      <li> some item4 </li>
      <li> some item4 </li>
      <li> some item4 </li>
    </ul>
  </div>

That is calling the background program gets more data which should append to that last item of . I am not sure how to convert string to node

   document.getElementById("ulgallery").appendChild(node);

1 Answer 1

1

Set the fetched string as the innerHTML of a dummy div element. Then get the childNodes of the dummy div. This will do the trick. It will convert the string as an array of DOM nodes. Then just iterate through the list and append the nodes to the parent.

Pseudocode:

        var temp = document.createElement('div');
        temp.innerHTML = child; // child is the fetched string from ajax call in your case
        child = temp.childNodes;
        for(var i=0; i<child.length ; i++){
           parent.appendChild(child[i]); 
        }
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.