-2

Using jQuery how can I find and replace a word but keep the HTML? When I do replace() it returns a string with HTML stripped.

This is an example. I want to replace Hello with Hi but when I use this code the result is stripping away all the HTML. I know I can simply wrap the text with a unique selector, but it's not possible to edit the HTML.

(function($) {
  var string = $('.banner-message');

  string.each(function() {
    var replaced = $(this).text().replace(/Hello /, 'Hi ');
    $(this).html(replaced);
  });
})(jQuery);
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

.banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

.banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

.banner-message.alt button {
  background: #fff;
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="banner-message">
  Hello World
  <button>Change color</button>
</div>
<div class="banner-message">
  Hello World
  <button>Change color</button>
</div>

http://jsfiddle.net/n7L24ue5/

Thank you...

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
MonicaLI8
  • 31
  • 1
  • 1
  • 4
  • No, I can't. Just edited the question. – MonicaLI8 Nov 06 '18 at 09:21
  • 1
    It's not stripping the HTML when you do `.replace` - `$(this).text()`returns you the *text* without the HTML. You then set the *text* as the *HTML*. – VLAZ Nov 06 '18 at 09:22
  • 1
    To add to the above, change `$(this).text().replace...` to `$(this).html().replace...`, just be careful not to replace any values which could be found *within* the HTML tags. – Rory McCrossan Nov 06 '18 at 09:23
  • Use `html()` instead of `text()` thanks guys! – MonicaLI8 Nov 06 '18 at 09:25
  • Also you can use [previousSibling](https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling) instead of using `replace()` – Mohammad Nov 06 '18 at 09:26

2 Answers2

0

You do

var replaced = $(this).text().replace(/Hello /, 'Hi ');
-----------------------^^^^

The .text() function returns the text version of the content. You need the method to get the html version :)
Hint: You already have the method you're looking for in that same snippet

Martijn
  • 15,791
  • 4
  • 36
  • 68
0

Add a span to your html, as so:

<div class="banner-message">
<span>Hello World</span>
  <button>Change color</button>
</div>

<div class="banner-message">
<span>Hello World</span>
  <button>Change color</button>
</div>

And update the javascript:

(function($) {
  var string = $('.banner-message');

  string.each(function(){
    var replaced = $(this).find('span').text().replace( /Hello /, 'Hi ' );
    $(this).find('span').html(replaced)
  });      
})(jQuery); 
mic
  • 4,300
  • 1
  • 19
  • 25