0

I have a rather interesting issue. Content is being build dynamically and creating the following scenario.

<button id="something">
    <i class="fa fa-refresh"></i>
    Some button text
</button>

What I'm trying to do is replace "some button text" while preserving the Font Awesome tag. I've tried regex and so on, but kind of stuck.

As a note, I've tried .text() and it does replace the text, but also removes the i tag too.

probie
  • 89
  • 10

1 Answers1

0

You can use contents() for getting all descendant including textNode . Iterate over the result using each() and update the nodVealue

$('#something').contents().each(function() {
  if (this.nodeType === 3)
    this.nodeValue = $.trim(this.nodeValue).length ? 'new value' : '';
    // if you want to remove the element then use 'this.remove()'
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="something">
  <i class="fa fa-refresh"></i>
  Some button text
</button>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188