Table of Contents

HTMLElement.new (static)

Creates a new HTMLElement of the given name.

Usage:
HTMLElement = HTMLElement.new(name, attrlist )
Member of:
HTMLElement (as static method)
Parameters:
  1. name – the name of the element to create (String, required).
  2. attrlist – a flat Object, containing a name-value pair for each attribute to add (optional).
  3. content – either a String, an HTMLElement, or a flat Array of HTMLElements to be inserted as the content of the new element (optional).
Returns:
The new HTMLElement.
Notes:
  • This is a static method of HTMLElement. It has to be called literally as HTMLElement.new(…) rather than on a reference to an existing element instance. Please see .appendNew() and .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

More information