Construct a new xjs_Element object.
the node to wrap
This wrapper’s node.
This element’s tag name in lowercase.
Append one or more tokens to this element’s [class]
attribute.
Argument(s) can also be space-separated tokens.
this.addClass('o-Object', 'c-Component') // add token(s) to the [class] attribute
this.addClass('o-Object c-Component', 'h-Helper') // spaces are allowed; they will just be split
this.addClass('') // do nothing; return `this`
this.addClass() // do nothing; return `this`
the classname(s) to add
this
ChildNode#after, but return this object when done.
This method exists simply for chaining.
the contents to insert after this node
this
Get and set attributes of this element.
If no argument is provided, this method does nothing and returns this
.
this
Get an attribute of this element.
If the key is a string and the value is not provided (or undefined
),
then this method returns the string value of the attribute identified by the key.
If the attribute exists but is a boolean attribute, the empty string ''
is returned.
If no such attribute exists, then null
is returned.
If the key is ''
, this method throws an error.
this.attr('itemtype') // get the value of the attribute (or `null` if it had not been set)
this.attr('') // throws, since `''` is not an attribute
the name of the attribute to get (nonempty string)
the value of the attribute specified (or null
if that attribute hasn’t been set)
Set or remove an attribute of this element.
If the key given is a string, and the value is a non-null ValueType type, then the attribute will be set (or modified) with the result of the value.
If the key is a string and the value is null,
then the attribute identified by the key is removed from this element.
If the value is NaN
, this method throws an error.
Notes:
checked=""
]), provide the empty string ''
as the value.this
, it can be chained, e.g.,my_elem.attr('itemscope', '').attr('itemtype','Thing').attr('itemprop', null)
However, it may be simpler to use an object argument:my_elem.attr({ itemscope:'', itemtype:'Thing', itemprop:null })
this.attr('itemtype', 'Person') // set an attribute (string)
this.attr('data-nthchild', 3) // set an attribute (number) (the value will be `"3"`)
this.attr('data-block', true) // set an attribute (boolean) (the value will be `"true"`)
this.attr('itemscope', '') // set a boolean attribute
this.attr('itemprop', null) // remove an attribute
this.attr('', 42) // throws, since `''` is not an attribute
this.attr('data-nthchild', NaN) // throws, since `NaN` is not permitted
the name of the attribute to set (nonempty string)
the value to assign to the attribute, or null
to remove it
this
Set or remove an attribute of this element, using a function.
If the key given is a string, and the value is a ValueFunction type, then the attribute will be set (or modified) with the result of the given function.
this.attr('data-id', function () { return this.id() }) // set an attribute using a function in this xjs.Element’s context
this.attr('data-id', function () { return this.id }, { id: 'custom-id' }) // set an attribute using a function in another given context
this.attr('' , function () {}) // throws, since `''` is not an attribute
this.attr('data-id', function () { return NaN }) // throws, since `NaN` is not permitted
the name of the attribute to set (nonempty string)
the function to call when assigning a value to the attribute
optionally pass in another object to use as this
inside the given function
this
Set or remove attributes of this element, using an object.
If an object is provided as the key, then no argument may be provided as the value.
The object must have values of the ValueType type;
thus for each key-value pair in the object, this method assigns corresponding
attributes. You may use this method with a single object argument to set and/or remove
multiple attributes (using null
to remove).
If the key is {}
or null
, this method does nothing and returns this
.
this.attr({ // set/remove multiple attributes all at once
itemprop : 'name',
itemscope: '',
itemtype : 'Person',
'data-id': null,
})
this.attr({}) // do nothing; return `this`
this.attr(null) // do nothing; return `this`
an object with ValueType type values
this
ChildNode#before, but return this object when done.
This method exists simply for chaining.
the contents to insert before this node
this
Get Element#className.
the value of the class
attribute, or null
if it had not been set
Set Element#className, and return this object when done.
This method exists simply for chaining. This method also takes arguments usable in xjs_Element.attr.
this.class('o-Object c-Component') // set the [class] attribute (string)
this.class(42) // set the [class] attribute (number) (the value will be `"42"`)
this.class(false) // set the [class] attribute (boolean) (the value will be `"false"`)
this.class('') // set the [class] attribute to the empty string: `[class=""]`
this.class(null) // remove the [class] attribute
the value to set for the class
attribute, or null
to remove it
this
Set Element#className, and return this object when done.
This method exists simply for chaining. This method also takes arguments usable in xjs_Element.attr.
this.class(function () { return this.tagName }) // set the [class] attribute using a function in this xjs.Element’s context
this.class(function () { return this.tagName }, { tagName: 'custom-name' }) // set the [class] attribute using a function in another given context
the function to call when assigning a value to the attribute
optionally pass in another object to use as this
inside the given function
this
Remove all child nodes from this node, and return it.
this
the function to call in the context of this
this
Get Element#id.
the value of the id
attribute, or null
if it had not been set
Set Element#id, and return this object when done.
This method exists simply for chaining. This method also takes arguments usable in xjs_Element.attr.
this.id('section1') // set the [id] attribute (string)
this.id(42) // set the [id] attribute (number) (the value will be `"42"`)
this.id(false) // set the [id] attribute (boolean) (the value will be `"false"`)
this.id('') // set the [id] attribute to the empty string: `[id=""]`
this.id(null) // remove the [id] attribute
the value to set for the id
attribute, or null
to remove it
this
Set Element#id, and return this object when done.
This method exists simply for chaining. This method also takes arguments usable in xjs_Element.attr.
this.id(function () { return this.tagName }) // set the [id] attribute using a function in this xjs.Element’s context
this.id(function () { return this.tagName }, { tagName: 'custom-name' }) // set the [id] attribute using a function in another given context
the function to call when assigning a value to the attribute
optionally pass in another object to use as this
inside the given function
this
the condition to evaluate; will be converted to boolean
the executable to call (in the context of this
) if the condition is truthy
the executable to call (in the context of this
) if the condition is falsy
this
Get Element#innerHTML.
the innerHTML
of this element
the html to set
this
Get Element#outerHTML.
the outerHTML
of this element
Set Element#outerHTML, and returns this object when done.
This method exists simply for chaining.
the html to set
this
Remove one or more tokens from this element’s [class]
attribute.
Argument(s) can also be space-separated tokens.
this.removeClass('o-Object', 'c-Component') // remove token(s) from the [class] attribute
this.removeClass('o-Object c-Component', 'h-Helper') // spaces are allowed; they will just be split
this.removeClass('') // do nothing; return `this`
this.removeClass() // do nothing; return `this`
classname(s) to remove
this
Replace a segment of this element’s class string with a new string segment.
Note: this is not the same as replacing one class token for another.
For that, use this.node.classList.replace(old_, new_)
.
let element = jsdom.JSDOM.fragment(`<i class="glyphicons glphicons-{{ icon }}"></i>`).querySelector('i')
new xjs.Element(element).replaceClassString('{{ icon }}', 'mobile')
element.outerHTML // <i class="glyphicons glphicons-mobile"></i>
the segment of this element’s [class]
attribute value to remove; might not be a complete token
the string with which to replace the removed segment
this
Execute a callback acting on this node, and then return this node.
Simplifies chaining when performing void tasks, especially tasks that have not been defined in this implementation. Note that this function is not asynchronous, and does not accept asynchronous arguments.
the function to call, taking this
as its only argument, and returning void
this
Execute a function based on a condition.
Given a condition, execute one function upon passing, or execute another upon failure. Note that this function is not asynchronous, and does not accept asynchronous arguments.
the condition to evaluate; will be converted to boolean
the callback to call (with this
as its parameter) if the condition is truthy
the callback to call (with this
as its parameter) if the condition is falsy
this
Get Node#textContent.
the textContent
of this node
Set Node#textContent, and returns this object when done.
This method exists simply for chaining.
the content to set
this
Remove all inner whitespace text nodes from this node, and return it.
let div = document.createElement('div')
div.innerHTML = `
<h1>
<em>Hello </em>
<b>Worl d</b>
</h1>
`
let trimmed_div = new xjs.Node(div).trimInner().node
return div.innerHTML === `
<h1>
<em>Hello </em>
<b>Worl d</b>
</h1>
`
&& trimmed_div.innerHTML = `<h1><em>Hello </em><b>Worl d</b></h1>`
this
Generated using TypeDoc
Wrapper for an Element.
https://www.w3.org/TR/dom/#element