Following HTML structure is given and cannot be changed:
<div id="cone" class="team">
<div class="default"></div>
...
<div class="default">
<img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional)
<img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional) ...
</div>
</div>
I want to get to the following result using jQuery:
<div id="cone" class="team">
<div class="default"></div>
...
<div class="default">
<img src="img.jpg" width="271" height="271" alt="" border="0">
<div class="desc">
First Text line<br> Second text line (optional)
</div>
<img src="img.jpg" width="271" height="271" alt="" border="0">
<div class="desc">
First Text line<br> Second text line (optional)
</div>
...
</div>
</div>
I have tried:
$(".team img").each(function () {
$(this.nextSibling).wrap('<div class="desc"></div>');
});
But this only wraps the first line:
<img src="fileadmin/dateien/bilder/team/platzhalter_team.jpg" width="271" height="271" alt="" border="0">
<div class="desc">First Text line</div>
<br>
Second text line (optional)
Important: There can be one line, two lines, even three lines of text after the <img>
Tag.
You need to iterate
br
element in.default
and add your new tag in loop. You can get previous/next text sibling of element usingNode.previousSibling
andNode.nextSibling
property.Edit:
If your html has multiple
<br>
, you need to replace br with custom text and after wrap, replace target text to<br>
. In example i used[br]
.