== .prependNew ==

.prependNew()

Prepends a new HTMLElement as the //first// child to an existing element.
Usage:
[[mdn>Web/API/HTMLElement|HTMLElement]] = Element.''prependNew''(name, attrlist)
Member of:
[[mdn>Web/API/HTMLElement|HTMLElement]]
Parameters:
- name – the name of the element to create (required). - attrlist – a flat [[mdn>Web/JavaScript/Reference/Global_Objects/Object|Object]], containing name-value pairs representing the attributes to be added to the new element (optional). - content – either a [[mdn>Web/JavaScript/Reference/Global_Objects/String|String]], a [[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 newly created [[mdn>Web/API/HTMLElement|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 [[toolbox:element:HTMLElement_new:index|HTMLElement.new()]] for creating the new element. Please see there for more examples. ===== See also ===== * [[toolbox:element:htmlelement_new:index|HTMLElement.new()]] * [[toolbox:element:appendnew:index|.appendNew()]] ===== More information ===== * [[mdn>Web/API/Node/appendChild|Node: appendChild() method]] on MDN. * [[mdn>Web/API/Document/createElement|Document: createElement() method]] on MDN.