Function wrap

  • The .wrap() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.

    Example

    const redFruit = $('<div class="red-fruit"></div>');
    $('.apple').wrap(redFruit);

    //=> <ul id="fruits">
    // <div class="red-fruit">
    // <li class="apple">Apple</li>
    // </div>
    // <li class="orange">Orange</li>
    // <li class="plum">Plum</li>
    // </ul>

    const healthy = $('<div class="healthy"></div>');
    $('li').wrap(healthy);

    //=> <ul id="fruits">
    // <div class="healthy">
    // <li class="apple">Apple</li>
    // </div>
    // <div class="healthy">
    // <li class="orange">Orange</li>
    // </div>
    // <div class="healthy">
    // <li class="plum">Plum</li>
    // </div>
    // </ul>

    See

    https://api.jquery.com/wrap/

    Type Parameters

    Parameters

    Returns Cheerio<T>