javascriptjquerywrapall

wrapAll and andSelf not wrapping self element


I'm using the code below:

$('.graph-img').each(function(index) {
    $(this).prev('h2,h3,h4,h5,h6,p').prevUntil('.graph-img').andSelf().not('h1').wrapAll("<div class='prevent-break'>");
});

It's wrapping all the elements, but excluding .graph-img that this currently refers to. Any ideas why?


Solution

  • andSelf doesn't add the first set of elements, but the previous one.

    You may use .add(this) instead here.

    Note that andSelf has been deprecated in favor of addBack whose name is less misleading.