While working on a project I ran across a bug and found this unexpected behaviour:
If I call .replace()
on a string and set the result to a div using .text()
the replace function works as I expect.
However, if I call .replace()
on a string and set the result to a div using .html()
, the targeted text is not replaced in the string.
Here's an example of what I mean:
$('#result1').text('¤cyId'.replace('¤cyId','¤cyId')); // works
$('#result2').html('¤cyId'.replace('¤cyId','¤cyId')); // doesnt work
var result = '¤cyId'.replace('¤cyId','¤cyId')
$('#result3').text(result); // works
$('#result4').html(result); // doesnt work
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result1"></div><br>
<div id="result2"></div><br>
<div id="result3"></div><br>
<div id="result4"></div>
I see that using .text()
instead of .html()
resolves the issue, but...
Why does this happen?