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

  • SEARCH
  • Home
  • 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 8125803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:56:09+00:00 2026-06-06T06:56:09+00:00

If I need to create a couple of nested DOM elements, I know one

  • 0

If I need to create a couple of nested DOM elements, I know one way to do it, is to write them as long string and then place them in the document using a suitable jQuery function. Something like:

elem.html(
'<div class="wrapper">
    <div class="inner">
        <span>Some text<span>
    </div>
    <div class="inner">
        <span>Other text<span>
    </div>
</div>'); 

This way is obviously not the cleanest. The sting doesn’t take too long to get messy and editing becomes a problem. I much more prefer this notation:

$('<div></div>', {
    class : 'inner'
})
.appendTo( elem );

The problem is I don’t know how to implement it efficiently when creating nested elements on the fly like above. So if there is way to do the 1st example with the 2nd notation, I’ll be glad to learn about it.

Basically, the question is, whats the best way to create nested HTML elements on the fly, without having to deal wit messy long strings?

Note : I am aware of templating engines. However this is a question concerning the creation of just a couple of HTML elements on the fly. Like while building the DOM dependencies for a plugin or similar cases.

  • 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-06T06:56:12+00:00Added an answer on June 6, 2026 at 6:56 am

    write them as long string and than place them in the document using a
    suitable jQuery function. Something like:

    The problem with this approach is that you’ll need a multi-line string – something Javascript doesn’t support – so in reality you’ll end up with:

    elem.html(
    '<div class="wrapper">'+
        '<div class="inner">'+
            '<span>Some text<span>'+
        '</div>'+
        '<div class="inner">'+
            '<span>Other text<span>'+
        '</div>'+
    '</div>');
    

    Using the method you suggested above, this is about as clean as I could manage to get it:

    elem.append(
        $('<div/>', {'class': 'wrapper'}).append(
            $('<div/>', {'class': 'inner'}).append(
                $('<span/>', {text: 'Some text'})
            )
        )
        .append(
            $('<div/>', {'class': 'inner'}).append(
                $('<span/>', {text: 'Other text'})
            )
        )
    );
    

    The other advantage to doing this is that you can (if desired) get direct references to each newly created element without having to re-query the DOM.

    I like to write polyglots, so to make my code re-usuable I usually do something like this, (as jQuery’s .html() doesn’t support XML):

    // Define shorthand utility method
    $.extend({
        el: function(el, props) {
            var $el = $(document.createElement(el));
            $el.attr(props);
            return $el;
        }
    });
    
    elem.append(
        $.el('div', {'class': 'wrapper'}).append(
            $.el('div', {'class': 'inner'}).append(
                $.el('span').text('Some text')
            )
        )
        .append(
            $.el('div', {'class': 'inner'}).append(
                $.el('span').text('Other text')
            )
        )
    );
    

    This isn’t very different to method #2 but it gives you more portable code and doesn’t rely internally on innerHTML.

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

Sidebar

Related Questions

I have a couple of numerical datasets that I need to create a concept
I need create clone repository. but I do not know where can I get
I need to create a script that backups a couple of files on a
We need to create a couple of pages for our WebSphere Portal 7 installation
I need to create native SQL query with couple of unions and subqueries. It'll
I need to create charts for WP Mango. Therefore I have a couple of
I have a need to create a couple of classes that will serve as
I would like to know the best way to deal with long running processes
I'm trying to create a gem with Bundler, following this guide: http://rakeroutes.com/blog/lets-write-a-gem-part-one/ . In
I need create custom dialog and put JPanel into it. Is it possible?

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.