- name – the name of the element to create ([[mdn>Web/JavaScript/Reference/Global_Objects/String|String]], required).
- attrlist – a flat [[mdn>Web/JavaScript/Reference/Global_Objects/Object|Object]], containing a name-value pair for each attribute to add (optional).
- content – either a [[mdn>Web/JavaScript/Reference/Global_Objects/String|String]], an [[mdn>Web/API/HTMLElement|HTMLElement]], or a flat [[mdn>Web/JavaScript/Reference/Global_Objects/Array|Array]] of [[mdn>Web/API/HTMLElement|HTMLElement]]s to be inserted as the content of the new element (optional).
Returns:
The new [[mdn>Web/API/HTMLElement|HTMLElement]].
Notes:
* This is a static method of [[mdn>Web/API/HTMLElement|HTMLElement]]. It has to be called literally as ''HTMLElement.new(…)'' rather than on a reference to an existing element instance. Please see [[toolbox:element:appendnew:index|.appendNew()]] and [[toolbox:element:prependnew:index|.prependNew()]] to create new elements as children of existing instances.
===== Examples =====
Simple example:
let foo = HTMLElement.new('div');
Example with attributes:
let bar = HTMLElement.new('a', {
'href': 'http://jmini.nuropa.eu/',
'class': 'bar',
'target': '_blank',
'hreflang': 'en'
});
Example with text content:
let btn = HTMLElement.new('button', {
'type': 'submit'
}, "Submit");
Example with HTMLElement content:
let p = HTMLElement.new('p', {},
HTMLElement.new('i', {}, "Some italic text."));
Example with an HTMLElement array:
let p = HTMLElement.new('ul', {}, [
HTMLElement.new('li', {}, "First"),
HTMLElement.new('li', {}, "Second"),
HTMLElement.new('li', {}, "Third"),
]);
===== See also =====
* [[toolbox:element:appendnew:index|.appendNew()]]
* [[toolbox:element:prependnew:index|.prependNew()]]
===== More information =====
* [[mdn>Web/API/HTMLElement|HTMLElement]] on MDN.