Search code examples
javascriptjquerycheerio

Create new element in cheerio


What is the best way to create a new element in cheerio?

May be:

cheerio.load('<li>something in here!</li>')

or

$('li', '<li>bla, bla, bla, ...</li>')

Solution

  • Neither load nor the context string actually create HTML. They serve as a starting point for creating a virtual DOM.

    Refer to the Manipulation section in the documentation for methods of injecting into the DOM. Namely, the following methods are provided for injection:

    • append
    • appendTo
    • prepend
    • prependTo
    • after
    • insertAfter
    • before
    • insertBefore
    • replaceWith
    • html
    • wrap