0

When changing the content with .html() there is an issue not showing the whole data:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

  <script>

    $(document).ready(function() {
      $("#btn2").click(function() {
        $("#test2").html("<b>Hello < world!</b>");
      });

  </script>
</head>

<body>
  <p id="test2">Result here</p>
  <button id="btn2">Click for result</button>
</body>

</html>

The result I get is: Hello

I need the result to be: Hello < world!

This is just a sample.

Shidersz
  • 16,846
  • 2
  • 23
  • 48
Amee
  • 337
  • 4
  • 14

4 Answers4

3

You can use &lt; in your snippet to be explicit but that was not your issue, your JS issue was not closing of jQuery.ready function in your snippet.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("#btn2").click(function(){
        $("#test2").html("<b>Hello &lt; world!</b>");
    });
  });
</script>
</head>
<body>
    <p id="test2">Result here</p>
    <button id="btn2">Click for result</button>
</body>
</html>
Rikin
  • 5,351
  • 2
  • 15
  • 22
2

You can use HTML entity &lt; to replace the < symbol.

$(document).ready(function()
{
    $("#btn2").click(function()
    {
        $("#test2").html("<b>Hello &lt; world!</b>");
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="test2">Result here</p>
<button id="btn2">Click for result</button>

If you don't have control over the input text, then you can Encode html entities in javascript. As mentioned on that link, you can create a method to encode the input str:

function encodeStr(str)
{
    return str.replace(
        /[\u00A0-\u9999<>\&]/gim,
        i => '&#' + i.charCodeAt(0) + ';'
    );
}

$(document).ready(function()
{
    $("#btn2").click(function()
    {
        let text = "Hello < world!";
        $("#test2").html("<b>" + encodeStr(text) + "</b>");
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="test2">Result here</p>
<button id="btn2">Click for result</button>
Shidersz
  • 16,846
  • 2
  • 23
  • 48
0

This fiddle suggests your code works (to a degree): https://jsfiddle.net/6y2s0ue9/

You may just want to correct the $(document).ready to close, like this:

$(document).ready(function(){
    $("#btn2").click(function(){
        $("#test2").html("<b>Hello < world!</b>");
    });
});

On click of the button, it replaces "Result here" with "Hello < world!"

NickZA
  • 321
  • 1
  • 6
0

With .html() the code will read the < symbol as a tag so it will print nothing so other answers can solve that.. But with .text() it will read as it is so no need to replace symbol with &lt;

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("#btn2").click(function(){
        $("#test2").html($('<b></b>').text("Hello < world!"));
    });
  });
</script>
</head>
<body>
    <p id="test2">Result here</p>
    <button id="btn2">Click for result</button>
</body>
</html>
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28