5
<div class="pagination">
  <a href="#">1</a>
  <a href="#">2</a>
  3
  <a href="#">4</a>
</div>

I need to use jQuery to wrap an HTML element around the number "3". I can't modify the HTML because it is dynamically generated.

1

3 Answers 3

15

I had a similar question which wraps all stray text

$(function() {

    var test = $('.pagination')
        .contents()
        .filter(function() {
            return this.nodeType === 3 && $.trim(this.nodeValue) !== '';
        })
        .wrap('<span/>');

});​

just had a minor confusion with wrap() and wrapAll(). here's a demo

Sign up to request clarification or add additional context in comments.

2 Comments

it changed the order of the elements jsbin.com/uvigam/edit#javascript,html
it's now updated. this time, i have a demo. i got confused with wrap() and wrapAll().
4

this will wrap all your number with a span, so the structure will be :

<a><span>1</span></a>

and the selected one only span without a :

<span>2</span>


var html = $('.pagination').html();
    html = html.replace(/[0-9]/g,"<span>$&</span>");
    $('.pagination').html(html);

so you can easily style it with :

.pagination a span {normal style} 
.pagination span{selected style}

2 Comments

@user560756 or you won't be notified.
why not simply $('.pagination').html('<span>'+$('.pagination').html()+'</span>');
2

This is old, but a shorter, simpler way to do this in my opinion would be:

$("div:not(a)").wrapInner("<a href='#'></a>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.