Table of Contents

<HTMLElement>.prependNew()

Prepends a new HTMLElement as the first child to an existing element.

Usage:
HTMLElement = Element.prependNew(name, attrlist)
Member of:
HTMLElement
Parameters:
  1. name – the name of the element to create (required).
  2. attrlist – a flat Object, containing name-value pairs representing the attributes to be added to the new element (optional).
  3. content – either a String, a HTMLElement, or a flat Array of HTMLElements to be inserted as the content of the new element (optional).
Returns:
The newly created HTMLElement.
Notes:
Potentially unexpected behaviour: Different to most other methods, this one returns the new element that was created, not the one it was called on!

Examples

Simple example:

let foo = document.getElementById('foo');
foo.prependNew('div');

Example with attributes:

document.getElementById('foo')
  .prependNew('a', {
    'href': 'http://code.kolmio.com/jmini/',
    'class': 'bar',
    'target': '_blank',
    'hreflang': 'en'
  }
);

Note: This function relies on HTMLElement.new() for creating the new element. Please see there for more examples.

See also

More information