Reputation: 333
<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.
Upvotes: 3
Views: 289
Reputation: 352
Your logic is wrong, because, the code append div element after the element that has class "datum" until <p>
element is start. But in div with class="bodytext" you have no <p>
element.
Try this;
$(".bodytext").each(){
$(this).after($(".datum")).wrapAll("<div class='someClass'>Some text <a href='path/to/link' title='some title' class='download'>Some text</a>. Again some text!</div>");
}
I'm not sure about wrapAll, but the order is true for the append operation.
Upvotes: 0
Reputation: 28513
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 bodytext
containing 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>
Upvotes: 3
Reputation: 4241
Here is an aproach that is faster than any full jQuery solution.
It fecthes the first element, removes it and re-creates the content with the remaining content left.
Here's what I mean:
$(function(){
$('.bodytext').each(function(){
var first = this.children[0];
this.removeChild(first);
this.innerHTML = first.outerHTML + '<div class="someclass">' + this.innerHTML + '</div>';
});
});
<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>
The outerHTML
property contains the whole HTML of the input, including:
name
id
class
data-*
on<event>
... any attribute directly set into the HTML
Hope this works for you.
Upvotes: 0
Reputation: 32354
try:
$('.bodytext').find('*').not('span').wrap('<div class="someClass" />')
Upvotes: 0