Elementals.js

Latest Version: 3.0 RC 1 17 July 2017

Method _.Node.write

Summary:

Allows you to write new text content or a new Element to an existing DOM Element.

Calling Convention:
_.Node.write(Element, content[, place])
Parameters:
Element
The target Element to which the content is added to or placed around.
content
If content is an object, it will be added directly. If not, it will be run through document.createTextNode before being added.
place optional

Where the element will be placed. If place is omitted or invalid, 'last' is the default.

'after'
content will be placed after the Element as a sibling
'before'
content will be placed before the value as a sibling
'first'
content will be placed before the Element.firstChild
'last' default
content will be placed after Element.lastChild
'replace'
content will replace the passed Element
'rewrite'
The passed Element will have all of its existing nodes (text, comments, elements) deleted (see _.Node.flush) and replaced by content
Returns:
Nothing

Example #1

HTML

<h1>Test</h1>

JavaScript

var
	h1 = document.getElementsByTagName('h1')[0],
	h2 = document.createElement('h2');
_.Node.write(h1, "\r\nAdded after the H1\r\n", 'after');
_.Node.write(h1, "Added before the H1\r\n", 'before');
_.Node.write(h1, "\r\nAdded before H1 content\r\n", 'first');
_.Node.write(h1, h2, 'after');
_.Node.write(h1, "\r\nAdded after H1 content\r\n", 'last');
_.Node.write(h2, 'Second Heading');

RESULT HTML (equivalent)

Added before the h1
<h1>
Added before H1 content
Test
Added after H1 content
</h1>
Added after the H1
<h2>Second Heading</h2>

In practice much of the above would actually be coded using _.make. In fact, several of _.make's attributes Object properties call this method.