4

I want to clear out the "normal text", but no glyphicon's.

The important code sections and its results:

  1. $("#btn_test").html() will give me:

    "<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Speichern &amp; schließen"

  2. $("#btn_test").text() will give me:

    " Speichern & schließen"

I just want to delete the text "" Speichern & schließen", without .replace(...)-function (because the text is dynamic).

I tried $("#btn_test").text(""), but this will clear also my span element.

Thank you.

Updated('possible duplicate'): Yes, it is the same question like here, but not the answer of Chris Davis, which solved my problem.

Community
  • 1
  • 1
99999
  • 141
  • 7
  • The text(, which needs to be deleted) can be before and after my `span` element. – 99999 Dec 07 '15 at 13:07
  • 1
    Possible duplicate of [How to remove text (without removing inner elements) from a parent element using jquery](http://stackoverflow.com/questions/11633610/how-to-remove-text-without-removing-inner-elements-from-a-parent-element-using) – Kaiido Dec 07 '15 at 13:17
  • 1
    Well now you've got an accepted answer you can still close it as dupe. Future answers might be very similar, no need to keep it open (this won't block votes nor delete your question). – Kaiido Dec 07 '15 at 13:49

2 Answers2

5

Remove the text node, e.g:

$('#btn_test').contents().filter(function(){
  return this.nodeType === 3;
}).remove();

Or simpler, if it's always the last:

$('#btn_test').contents().last().remove();

-jsFiddle-

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
2

Simplest way would be to wrap your text in a span — it then becomes trivial to remove this text.

$(document).ready(function() {
 $('#btn_test .text').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn_test">
  <span class="glyphicon glyphicon-floppy-disk"
        aria-hidden="true"></span> 
  <span class="text">Speichern &amp; schließen</span>
</button>

jsFiddle link

Chris Davis
  • 433
  • 2
  • 10