Replace ignoring tag and returning as string

1

I think I've seen this problem here once, but I could not find it at all or search the internet (I forgot what words to use in the search because I think I've dealt with it before but it gave me a total blank). >

I'm making a replace of a text in a div and inserting some words between the <b></b> tags.

For example:

<div id="texto">
    Qualquer texto aqui
</div>

I want the word "text" to be between <b></b> (bold):

var texto_original = $("#texto").text();
$("#texto").text(texto_original.replace("texto","<b>texto</b>"));

It turns out that after replace the text in div looks like this:

Qualquer <b>texto</b> aqui

When it would look like this:

  

Any text here

See the example:

var texto_original = $("#texto").text();
$("#texto").text(texto_original.replace("texto","<b>texto</b>"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="texto">
   Qualquer texto aqui
</div>

That is, the tag is being treated as a string. If I am not mistaken there is a method that does this conversion, but I am not remembering at all. How do I resolve this?

    
asked by anonymous 16.02.2018 / 17:49

1 answer

4

To be treated as html has to be assigned with the function html :

var texto_original = $("#texto").html();
$("#texto").html(texto_original.replace("texto","<b>texto</b>"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="texto">
   Qualquer texto aqui
</div>

The idea of text is precisely this, to prevent what is put as content is interpreted as html. If the user enters the content, text ends up protecting against javascript injection.

    
16.02.2018 / 17:56