Edit this page

up.element up.element.affix(parent, [position], selector, [attrs])
JavaScript function

Creates an element matching the given CSS selector and attaches it to the given parent element.

To create a detached element from a selector, see up.element.createFromSelector().

Use up.hello() to activate JavaScript behavior within the created element.

Example

element = up.element.affix(document.body, '.klass')
element.parentElement // returns document.body
element.className // returns 'klass'

See up.element.createFromSelector() for many more examples.

Parameters

parent Element

The parent to which to attach the created element.

[position='beforeend'] string optional

The position of the new element in relation to parent. Can be one of the following values:

  • 'beforebegin': Before parent, as a new sibling.
  • 'afterbegin': Just inside parent, before its first child.
  • 'beforeend': Just inside parent, after its last child.
  • 'afterend': After parent, as a new sibling.
selector string

The CSS selector from which to create an element.

[attrs] Object optional

An object of attributes to set on the created element.

[attrs.text] Object optional

The text content of the created element.

[attrs.content] Object optional

The inner HTML of the created element.

[attrs.style] Objectorstring optional

An object of CSS properties that will be set as the inline style of the created element.

The given object may use kebab-case or camelCase keys.

Return value

The created element.