-1

I have a code here:

<a href="#"><i class="flaticon-basket9"></i> addtobasket</a>

I want remove the "addtobasket" text .

Trying using jquery code:

$(function(){
    $('a').text(' ');
    })

but that code removed <i> tag also.just want to remove the text.

the "addtobasket" text may change and that not constant

Hamed mayahian
  • 2,465
  • 6
  • 27
  • 49

4 Answers4

3

You need to iterate over the nodes within the anchor, checking for a matching text node:

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

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
2

Use:

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

You can also use:

$('a i').prop('nextSibling').value = '';
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

Try

$("a").html($('a').children());
Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
0

$(document).ready(function() {
  console.log($('a>span').html());
  $('a>span').html('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<a href="#"><i class="flaticon-basket9"></i> <span>addtobasket</span></a>
indubitablee
  • 8,136
  • 2
  • 25
  • 49