Skip to content Skip to sidebar Skip to footer

Surround 2 Sibling Elements With A Span

Suppose I have this HTML
Some textFROM HERE

Solution 1:

You can wrap a collection of elements in jQuery, by using .wrapAll() and .unwrap():

Fiddle Demo

Adding highlight:

var$wrapped = highlight($start, $end, '.foo'); // highlight and get the highlighted items collection

Removing highlight:

$wrapped.unwrap(); // use unwrap on the collection to remove highlight

Highlight func:

functionhighlight($start, $end) {
    /** return the wrapped collection **/return$start
        .nextUntil($end) // get elements between $start and $end
        .addBack() // add $start back
        .add($end) // add $end
        .wrapAll("<span class='highlight' />"); // wrap them with highlight
}

Post a Comment for "Surround 2 Sibling Elements With A Span"