A very simple module that helps you build html structures programmatically.
Using npm, simply run:
npm install html-object --save
Usage is pretty straight-forward. Simply instantiate a new HtmlObject
and start creating awesome-sauce.
var HtmlObject = require('html-object') // Require HtmlObject
, list = new HtmlObject('ul'); // Create a new instance.
// Add some children
list.spawnChild('li').addClass('list-item').setContent('I am an item!');
list.spawnChild('li').addClass('list-item').setContent('And so am I!');
list.spawnChild('li').addClass('list-item').setContent('Look at us being sassy!');
// Now let's get some output.
console.log(list.render());
// <ul><li class="list-item">I am an item!</li><li class="list-item">And so am I!</li><li class="list-item">Look at us being sassy!</li></ul>
An HtmlObject that renders to markup.
[tag]: string, The tag for the new element. E.g. "div" or "span".
[attributes]: Object, An object of attributes. E.g. {id:"myId"}
Set if this element is void. This is useful for custom elements that you think should be void (think angularjs).
isVoid: boolean, Boolean indicating if this element should, or should not be void.
HtmlObject, Fluent interface
Returns whether or not this element's, or the supplied tag is void.
[tag]: string, The tag you wish to check is void.
boolean, If this element, or the supplied tag is considered void.
Returns whether or not this element is void
boolean, If this element is Xhtml
Set whether or not this element is Xhtml.
boolean: boolean, True to set to xhtml, false to set to html x.
HtmlObject, Fluent interface
Get this element's tag name
string, The tag of this element
Remove a specific attribute.
attribute: string, The name of the attribute to remove.
HtmlObject, Fluent interface
Get this element's attributes.
Object, The element's attributes.
Get a specific attribute.
attribute: string, The name of the attribute you wish to get.
string|null, The value of the attribute, or null when not set.
Set (and overwrite) the attributes.
attributes: Object, An object of attributes to set.
HtmlObject, Fluent interface
Add multiple attributes.
attributes: Object, An object of attributes to add.
HtmlObject, Fluent interface
Set a specific attribute.
attribute: string, The name of the attribute you wish to set.
value: string, The value of the attribute.
HtmlObject, Fluent interface
Set content placement to "append". this will append the content to the body after the child elements.
HtmlObject, Fluent interface
Set content placement to "prepend". this will prepend the content to the body before the child elements.
HtmlObject, Fluent interface
Convenience method. Add an array of multiple classes at once.
classes: Array, The classes to add.
HtmlObject, Fluent interface
Convenience method. Add a class to the element.
className: string, The class to add.
HtmlObject, Fluent interface
Convenience method. Remove a class from the element.
className: string, The class to remove.
HtmlObject, Fluent interface
Set (and overwrite) content.
content: string, The content to set
HtmlObject, Fluent interface
Add (append) content.
content: string, The content to append to the existing content.
HtmlObject, Fluent interface
Add (prepend) content.
content: string, The content to prepend to the existing content.
HtmlObject, Fluent interface
Clear (remove) the content.
HtmlObject, Fluent interface
Render the attributes to a string.
string, A rendered string of attributes.
Convenience method. Set data-something.
key: string, The key for the data attribute you wish to set
value: string, The value to set for key.
HtmlObject, Fluent interface
Convenience method. Get data-something.
key: string, The key for the data attribute you wish to get.
string, The value of the data attribute.
Convenience method. jQuery-like syntax for data.
key: string, The key for which you wish to get or set the value.
[value]: string, The value to set for key.
HtmlObject|string, Fluent interface, or data value on get.
Convenience method. Remove data-something.
key: string, Remove the data-something value.
HtmlObject, Fluent interface
Render the children for this element.
string, The rendered string.
Add a child to this element
child: HtmlObject, The child instance.
HtmlObject, Fluent interface
Spawn a new child for this element.
[tag]: string, The tag for the child element.
[attributes]: Object, An object of properties for the child element.
HtmlObject, Fluent interface} The child element
Render this element.
string, The rendered output.