dom-children

Functions for modifying DOM elements

npm install dom-children
15 downloads in the last week
107 downloads in the last month

dom-children

Library to modify DOM

Example:

add(document.body, '<h1>{title}</h1>{content}', {
  title: 'Hello',
  content: 'Welcome!'
})

Install

$ npm install dom-children

API

add(element, child)

Adds child to el

add(document.body, document.createElement('textarea'))
add('body .content', document.createElement('textarea'))
add('.content', '<div>hello</div>')
add('.content', '<h1>{title}</h1>', { title: 'Hello!' })

addAfter(element, child, reference)

Similar to addBefore

addBefore(element, child, reference)

addBefore(document.body, document.createElement('textarea'), document.body.firstChild)
addBefore('body', '<h1>{msg}</h1>', { msg: 'foobar' }, document.body.firstChild)

insert(element, parent)

insert element to parent as child

insert(document.createElement('textarea'), document.body)
insert('<input />', '.content')
insert('<h1>{title}</h1>', { title: 'hello' }, '.content')

replace(element, target, replacement)

replace target with replacement

replace(document.body, document.body.firstChild, document.createElement('textarea'))
replace('body .content', '.content ul', '<h1>hello</h1>')
replace('body .content', '.content ul', '<h1>{msg}</h1>', { msg: 'hello!' })

remove(element)

remove element

remove(document.body.firstChild)
remove('body .content')

remove(parent, child)

remove child

remove(document.body.firstChild, 'h1')
npm loves you