Jerry Svensson Jerry Svensson -4 years ago 81
jQuery Question

Move text inside an element to its child element

I want to move a text snippet inside the

label
to be inside the
a
element. I'm not a jQuery wizard and I don't understand how to use
appendTo
,
insertBefore
etc when I can't target the textsample. I want to keep the HTML intact except the move of the text.

<div class="column">
<div class="cart">
<div class="filter">
<label>
<a></a>
textsample
</label>
</div>
<div class="filter">
<label>
<a></a>
textsample
</label>
</div>
</div>



Answer Source

You can iterate the anchor element and use .append() to move the text.

$(".filter label a").each(function() {
  $(this).append(this.nextSibling); //Read the text node using plain JS property
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="column">
  <div class="xart">
    <div class="filter">
      <label>
        <a href="#"></a>textsample</label>
    </div>
    <div class="filter">
      <label>
        <a href="#"></a>textsample</label>
    </div>
  </div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download