Joe
Joe

Reputation: 1001

How to disable HTML tags in a div?

I have a page to show a log file, sometimes the log file contains some html tags and they make my page run slower since the tags are converted to contents like pictures etc., I want to disable that, so my goal is to show those tags as texts only, they should never be converted to contents.

This is the that I use:

<script type="text/javascript">
  $(document).ready(function() {
    setInterval(function() {
      $.ajax({
        url: "ssh.log",
        success: function(result) {
          $("#ssh").html(result);
        }
      });
      var textarea = document.getElementById("ssh");
      textarea.scrollTop = textarea.scrollHeight;
    }, 1000);
  });
</script>

And my div looks like this:

<div id="ssh"></div>

How can I accomplish that?

Upvotes: 1

Views: 158

Answers (3)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Use .text() to render content as a text:

$("#ssh").text(result);

Upvotes: 4

Ben Kolya Mansley
Ben Kolya Mansley

Reputation: 1793

As the previous answers have said, use .text() to render it as just plaintext. If for some reason you need to keep it as .html(), you can use simple string replacement to escape the tags.

result = result.replace('<','&lt;');
$('#ssh').html(result);

Upvotes: 1

Change:

$("#ssh").html(result);

to

$("#ssh").text(result);

Upvotes: 3

Related Questions