user1322093
user1322093

Reputation:

How can I wrap just a part of an element’s content using jQuery?

how can I wrap a part of a content when I have elements and normal text? I've tried .after() but this automaticaly adds a closing tag.

I have got this:

<div class="block">
    <a href="#">example 1</a>
    .
    <a href="#">example 1</a>
    .
    <a href="#">example 1</a>
    .
    <a href="#">example 1</a>
</div>

And I have to transform it to:

<div class="block">
    <a href="#">example 1</a>
    <div class="wrap">
        .
        <a href="#">example 1</a>
        .
        <a href="#">example 1</a>
        .
        <a href="#">example 1</a>
    </div>
</div>

Upvotes: 0

Views: 45

Answers (3)

Umur Kontacı
Umur Kontacı

Reputation: 35478

The others answers are much clearer while this gives you some spesificity

var target = $($(".block a").slice(1)); // drop 1st
var container = $("<div class='wrap' />");
container.insertBefore($(target[0]));
target.each(function () {
    $(this).appendTo(container);
});

Upvotes: 0

Ram
Ram

Reputation: 144709

$('.block').contents().filter(function(){
   return $(this).index() > 0;
}).wrapAll('<div class="wrap"></div>');

http://jsfiddle.net/uhzsN/

Upvotes: 2

Ricardus
Ricardus

Reputation: 739

$('.block a:gt(0)').wrapAll('<div class="wrap" />');

Upvotes: 0

Related Questions