Questions tagged [createelement]

createElement is a Web API that creates creates the specified HTML element in HTML,In a XUL document creates the specified XUL element,In other documents it creates an element with a null namespaceURI.

Description

  • In an HTML document creates the specified HTML element
  • In a XUL document creates the specified XUL element.
  • In other documents creates an element with a null namespaceURI.

Syntax

var element = document.createElement(tagName);

where,

  • element is the created element object.
  • tagName is a string that specifies the type of element to be created. The nodeName of the created element is initialized with the value of tagName.

References

  1. createElement - W3C Spec
  2. createElement - MDN Link
546 questions
256
votes
8 answers

The preferred way of creating a new element with jQuery

I've got 2 ways I can create a
using jQuery. Either: var div = $("
"); $("#box").append(div); Or: $("#box").append("
"); What are the drawbacks of using second way other than re-usability?
Ashwin
  • 12,081
  • 22
  • 83
  • 117
93
votes
5 answers

Advantages of createElement over innerHTML?

In practice, what are the advantages of using createElement over innerHTML? I am asking because I'm convinced that using innerHTML is more efficient in terms of performance and code readability/maintainability but my teammates have settled on using…
oninea
  • 1,423
  • 4
  • 14
  • 15
83
votes
7 answers

Javascript Append Child AFTER Element

I would like to append an li element after another li inside a ul element using javascript, This is the code I have so far.. var parentGuest = document.getElementById("one"); var childGuest = document.createElement("li"); childGuest.id = "two"; I…
Wez
  • 10,555
  • 5
  • 49
  • 63
81
votes
2 answers

Javascript: document.createElement('') & delete DOMElement

If you create a element within a function like: function makeDomElement() { var createdElement = document.createElement('textarea'); } And you do not append it anywhere in the DOM i.e. via .appendChild functions, does it still remain in memory?…
Gary Green
  • 22,045
  • 6
  • 49
  • 75
55
votes
3 answers

Creating a div element inside a div element in javascript

I'm trying a very basic example of creating a div inside an already existing div. It doesn't seem to be working when I use: document.getElementbyId('lc').appendChild(element) but works fine when I do this: document.body.appendChild(element) Do I…
Komal Waseem
  • 1,089
  • 3
  • 17
  • 24
41
votes
3 answers

How do I add a non-breaking whitespace in JavaScript without using innerHTML?

I'm generating content dynamically and in some instances, I need to set a   as the only content of a element. However, the following adds   as text vs adding a empty space: var foo = document.createElement("span") foo =…
frequent
  • 27,643
  • 59
  • 181
  • 333
33
votes
3 answers

Reactjs: Warning: React.createElement: type should not be null or undefined

this the full error Warning: React.createElement: type should not be null or undefined. It should be a string (for DOM elements) or a ReactClass (for composite components). and this is all I have in this view import React, { Component } from…
Non
  • 8,409
  • 20
  • 71
  • 123
24
votes
1 answer

javascript document.createElement or HTML tags

I am confused on following style of writing code. I want to know which is the practical and efficient method to insert HTML tag in document. Javascript: document.getElementById('demoId').innerHTML='
hello world and click me
blue ghhgdtt
  • 907
  • 4
  • 11
  • 16
23
votes
2 answers

Dynamically create option elements in Javascript

What is the preferred way in Javascript to dynamically create DOM option elements? I've found both the Option constructor and the createElement variant used in actual code like this: var option = new Option(text, value); and this: var option =…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
18
votes
2 answers

Javascript: onclick/onsubmit for dynamically created button

I dynamically create a button in the way I found in the Internet: Page = function(...) { ... }; Page.prototype = { ... addButton : function() { var b = content.document.createElement('button'); b.onclick = function() {…
Christian
  • 3,239
  • 5
  • 38
  • 79
18
votes
3 answers

createElement variable2

I have a requirement to create a navigation web part within SharePoint 2010. I am using a table to display the items from a SharePoint list and the table is structured as so: Column1 = The text to display (Title) Column2 = The URL (TitleLink) I…
frank billy
  • 355
  • 2
  • 5
  • 15
17
votes
3 answers

Should you add HTML to the DOM using innerHTML or by creating new elements one by one?

There are two methods to add HTML-code to the DOM and I don't know what is the best way to do it. First method The first way is the easy one, I could simply add HTML-code (with jQuery) using $('[code here]').appendTo(element); which is much like…
Harmen
  • 22,092
  • 4
  • 54
  • 76
16
votes
2 answers

javascript to create a button with onclick

I'm trying to use javascript to create a button that has a onclick event that calls a function defined in the head that takes in as parameter a dom object relative to the button. how do i do this? ex: