Move closing </a> tag to the end of a containing element

  • A+
Category:Languages

I'm trying to get a link to wrap around all text within a div. I can only find solutions where you move certain DOM elements entirely, or move other elements into an element.

current situation:

<div class="text">     <a href="link">text</a> and more text </div> 

desired situation:

<div class="text">     <a href="link">text and more text</a> </div> 

Unfortunately, I cannot change the markup, so I have to do something with jQuery.

 


Get text after anchor tag using nextSibling property

$(".text a").html(function(i, h){   return h + this.nextSibling.nodeValue; })[0].nextSibling.nodeValue = '';
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="text">   <a href="link">text</a> and more text </div>

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: