Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:22:57+00:00 2026-06-09T15:22:57+00:00

Ok, people, I need your help. I’ve found some code here on Stackoverflow (can’t

  • 0

Ok, people, I need your help. I’ve found some code here on Stackoverflow (can’t find that link) which generate HTML code dynamically via JS. Here is code:

function create(htmlStr) {
    var frag = document.createDocumentFragment(),
        temp = document.createElement('div');
    temp.innerHTML = htmlStr;
    while (temp.firstChild) {
        frag.appendChild(temp.firstChild);
      }
        return frag;
    }

    var fragment = create('<div class="someclass"><a href="www.example.com"><p>some text</p></a></div>'); 

    document.body.insertBefore(fragment, document.body.childNodes[0]);

This code works just fine! But the generated code appears on top of page, right below body tag. I would like to generate that code inside empty div with specific id="generate-here".

So output will be:

<div id="generate-here">
    <!-- Here is generated content -->
</div>

I know that I can’t see generated content with “view source”. I only need to generate that content just in this particular place with #generate-here. I’m Javascript noob, so if anyone can just rearrange this code that will be perfect. Thanks a lot!

P.S. I know how to do this with Jquery, but I need native and pure JavaScript in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:22:58+00:00Added an answer on June 9, 2026 at 3:22 pm

    All you need to do is change the last line. This will add the created element as the last child of the div:

    document.getElementById("generate-here").appendChild(fragment);     
    

    This will add the created element as the first child of the div:

    var generateHere = document.getElementById("generate-here");
    generateHere.insertBefore(fragment, generateHere.firstChild);
    

    You can also use innerHTML to just replace everything with new text (as you do in your create function). Obviously this one doesn’t require you to keep the create function because you need an html string instead of a DOM object.

    var generateHere = document.getElementById("generate-here");
    generateHere.innerHTML = '<div class="someclass"><a href="www.example.com"><p>some text</p></a></div>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everywhere I see, can't find any info about it. I need that people who
I need your help. I have a php script to send email to people
Greetings good people - I need some help. I have until now buildt web-pages
I realize that I need to use a mailserver so people can contact me,
I noticed that when doing code view, people here in my company usually just
I'm doing what a lot of people probably need to do, processing tasks that
I need to find an open source alternative to VSS where people will share
Just need a little bit of Javascript to warn people that press the back
i really really need your help. I'm successfully setting up a connection to my
I'm creating a website for a client in which he wants a link that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.