How to create deep object with dynamic name in Javascript?

How do you create an element with JavaScript without its literal name being displayed on the page?

  • By "literal name" I mean, for example:  [object HTMLDivElement]

  • Answer:

    I'm not at all sure what you're doing that does show the toString on the page, but here's some plain-old-js you can use to insert a new <div> as a child of the <body>. ;(function() { var div = document.createElement( 'div' ); // here you might want to add a class or an id or some additional content document.body.appendChild( div ); })(); However, it's probably a better idea for you to use something like jQuery for DOM manipulation.  With jQuery, it would be just be a matter of: $( document.body ).append( '<div></div>' );

Jacob Rothstein at Quora Visit the source

Was this solution helpful to you?

Other answers

It appears you're creating the element correctly, but then inserting into the page incorrectly, perhaps using innerHTML or innerText. Use element.appendChild(newElement) or similar methods instead.

Tom Robinson

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.