1

I have a div which I am appending text to. I can remove the text from the div but it removes all the text. I only want to remove specific text and keep the other text. As this is dynamic, so I will not be able to use id's or classes for the text.

I have tried searching for the text using the following.

$('#test-div:contains("'+test2+'")').text('');


var test1 = 'Hello World!';
var test2 = 'Good bye World';

$('#test-div').append(test1 +' '+ test2);
$('#test-div:contains("'+test2+'")').text('');

<div id = "test-div"></div>

It should just remove the variable text but removes it all. I would like it to remove just that variable text. I have googled to try and find a solution but I was unable to find a solution.

kode master
  • 100
  • 1
  • 15
Brian Nowlan
  • 137
  • 1
  • 1
  • 7

2 Answers2

1

based on this qustion you can write code like this:

var test1 = 'Hello World!';
var test2 = 'Good bye World';

$('#test-div').append(test1 +' '+ test2);
$('#test-div').text(function(){
    var t = $(this).text().replace(test2, '')
    return t;
});

<div id = "test-div"></div>
Hamed
  • 313
  • 2
  • 15
0

Try String.replace

var str1 = "Hello World!";
var str2 = "Good bye World";

var text = $('#test-div').text();

var res = text.replace(str2, " ");

$('#test-div').text(res );
kode master
  • 100
  • 1
  • 15