Menu Developer Moovweb University

Global Namespace

tag(name, [attribs], [content])
moov_cheerio
Source (L23)

Creates a new element. Note that this is not a Cheerio object: you should wrap it in `$()` if Cheerio methods need to be invoked on it.

Parameters

Name Type Description
name String A string for the name of the element.
[ attribs ] Object Optional. An object containing the attribute-value pairs of the new element.
[ content ] String Optional. A string for the text node for the new element.

Return Value

An object associated with the newly-created DOM element. defined by the arguments passed in (denoted by a `type` value of "tag").

Examples

let grape = tag("li", {class: "grape"}, "Grape");
grape.insertAfter(".apple");
// => Throws: TypeError (grape is not a Cheerio object, and the object
//    associated with the grape DOM element does not have an insertAfter
//    method defined)
$body.find("ul").append(grape);
// => Note: This is OK - you can append a non-Cheerio object here
// => HTML output:
//    <ul id="fruits">
//      <li class="apple" data-which="fuji">Apple</li>
//      <li class="grape">Grape</li>
//      <li class="pear">Pear</li>
//      <li class="orange">Orange</li>
//    </ul>
let $grape = $(tag("div", {class: "grape"}, "Grape"));
$grape.insertAfter(".apple");
// => Returns: original Cheerio object associated with the newly-created
//    `.grape` element
// => HTML output:
//    <ul id="fruits">
//      <li class="apple" data-which="fuji">Apple</li>
//      <li class="grape">Grape</li>
//      <li class="pear">Pear</li>
//      <li class="orange">Orange</li>
//    </ul>

Back to top


txt(content)
moov_cheerio
Source (L78)

Creates a new text node.

Parameters

Name Type Description
content string A string for the content that the text node will contain.

Return Value

An object associated with the newly-created text node defined by the `content` argument passed in (denoted by a `type` value of "text").

Example

$body.find(".apple").append(txt(", more text"));
// => HTML output:
//    <ul id="fruits">
//      <li class="apple" data-which="fuji">Apple, more text</li>
//      <li class="pear">Pear</li>
//      <li class="orange">Orange</li>
//    </ul>

Back to top


Last updated Tue Nov 15 2016 22:28:29 GMT+0000 (UTC)