javascriptjqueryhtmlwrapallnextuntil

How to wrap all HTML-Code after <span class="xy"> until next occurence of </p>?


<p class="bodytext">
    <span class="datum">19.11.2015:</span>Some text <a href="path/to/link" title="some title" class="download">Some text</a>. Again some text!
</p>
<p class="bodytext">
    <span class="datum">19.11.2015:</span>Maybe also only some text.
</p>
<p class="bodytext">
    <span class="datum">19.11.2015:</span><a href="path/to/link" title="some title" class="download">Only with link</a>
</p>

Should be after jQuery:

<p class="bodytext">
    <span class="datum">19.11.2015:</span>
    <div class="someClass">Some text <a href="path/to/link" title="some title" class="download">Some text</a>. Again some text!</div>
</p>
<p class="bodytext">
    <span class="datum">19.11.2015:</span>
    <div class="someClass">Maybe also only some text.</div>
</p>
<p class="bodytext">
    <span class="datum">19.11.2015:</span>
    <div class="someClass"><a href="path/to/link" title="some title" class="download">Only with link</a></div>
</p>

So all HTML-Code after the </span> should be wrapped in a div until the next occurence of </p>

What I tried:

$(".datum").each(function (index) {
        $(this).nextUntil("p").andSelf().wrapAll("<div class='someClass' />");
    });

Or:

$(".datum").nextUntil("p").wrap('<div class="someClass" />');

Didn't work either.


Solution

  • Try this: You can make use of wrapInner to wrap div around content of bodytext div and then take out datum span from wrapped div.

    $(function(){
        $('p.bodytext').each(function(){
        
          $(this).wrapInner( "<div class='someClass'></div>");
          $(this).prepend($(this).find('div.someClass').find('span.datum'));
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <p class="bodytext">
        <span class="datum">19.11.2015:</span>Some text <a href="path/to/link" title="some title" class="download">Some text</a>. Again some text!
    </p>
    <p class="bodytext">
        <span class="datum">19.11.2015:</span>Maybe also only some text.
    </p>
    <p class="bodytext">
        <span class="datum">19.11.2015:</span><a href="path/to/link" title="some title" class="download">Only with link</a>
    </p>

    EDIT: - As OP wants to put filter on bodytextcontaining child datum span. Here you can use :has selector for bodytext. Also you can use :contains or .has() jQuery methods.

    $(function(){
        $('p.bodytext:has(span.datum)').each(function(){
        
          $(this).wrapInner( "<div class='someClass'></div>");
          $(this).prepend($(this).find('div.someClass').find('span.datum'));
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <p class="bodytext">
        <span>19.11.2015:</span>Some text <a href="path/to/link" title="some title" class="download">Some text</a>. Again some text!
    </p>
    <p class="bodytext">
        <span class="datum">19.11.2015:</span>Maybe also only some text.
    </p>
    <p class="bodytext">
        <span class="datum">19.11.2015:</span><a href="path/to/link" title="some title" class="download">Only with link</a>
    </p>