Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just delete the normal text, but not the span element

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.

like image 915
99999 Avatar asked Oct 28 '25 09:10

99999


2 Answers

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-

like image 169
A. Wolff Avatar answered Oct 30 '25 23:10

A. Wolff


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

like image 41
Chris Davis Avatar answered Oct 30 '25 23:10

Chris Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!