Javascript print html tags as text

I'm trying to print plain html from javascript and can't figured out how to do so. Example:

$['#elem'].html['This is a normal string']; // --> This is a normal string

$['#elem'].html['This contains html']
//Current-> This contains html   [contains is bold]
//Wanted-> This contains html

I want to do this because I'm working with comments written by users and don't want html tags to apply if they put some in their comments. But for other reasons I need to see the tags.

Thanks for your help!

asked Apr 3, 2017 at 20:35

1

The following should work:

$['#elem'].text['This contains html'];

Related Snippet:

$['#an-id'].text['I don\'t want this to be HTML'];

This is not HTML!

Good luck!

Check the jQuery documentation for more information.

answered Apr 3, 2017 at 20:37

Use the jQuery .text[] function, which automatically escapes HTML to text for you!

In this example it is:

$['#elem'].text['This contains html']

answered Apr 3, 2017 at 20:36

AP.AP.

7,3942 gold badges23 silver badges32 bronze badges

you could use the jQuery .text[] method.

$['#elem'].text['This contains html'];

example:






$[document].ready[function[]{
    $["button"].click[function[]{
        $["#elem"].text['This contains html'];
    }];
}];




Click ME!!

This is a paragraph.

answered Apr 3, 2017 at 20:37

Ousmane D.Ousmane D.

53.3k8 gold badges84 silver badges118 bronze badges

You can show HTML tags as plain text in HTML on a website or webpage by replacing    with  > or &62; on each HTML tag that you want to be visible.

Ordinarily, HTML tags are not visible to the reader on the browser. They are there but you cannot see them.

However, through HTML entities you can display the HTML tags that are part of the HTML markup code that are not visible on a browser.

Example
So if you want to display:

This is a paragraph

on the browser, you write it as: <p> This is a paragraph </p>.

HTML has some special characters that make up the language; they are the reserved character. The reserved characters in HTML are < , > , ” and &. The browser will never display them since they have some meaning in HTML.

HTML entities are pieces of text used to display reserved characters, invisible characters[like space] and other non-keyboard characters.

Entities begin with the ‘&’ followed by ‘entity name’ or ‘entity number’ and end with the ‘;’ ie. &entity-name; or &entity-number;

Reserved characters in HTML

List of HTML entities

View the complete lists of HTML entities.

  1. The complete list of HTML entities with their numbers and names.
  2. Character Entity Reference Chart.
  3. HTML entities list

How to Quickly Replace Lots of < and > Fast

The quickest way to replace alot of [greater than] signs is to use an online HTML tags to entities converter. You copy and paste your HTML code, convert it and copy the resulting text.

Another method you can use is the search and replace feature available in all text editors. In my case, I am using Sublime Text editor.

To use this feature use “CTRL + H” or click on “FIND” on the menu bar and then click on “replace”. Add the symbol you want to replace on the find box and the entity you want to replace with on the replace box.

Then, click on replace or replace all.

I hope by now you can be able to display your HTML code on the web browser.

How to Preserve Code Formatting and Indentation

As by now, you have noted that the indentation on your HTML code has disappeared.
However, you can preserve the indentation by using the pre HTML tags. So wrap the code that you want to display online using the [

 pre html tags].


  Your HTML code goes in here.
      And can be indented.

HTML tags.

Example.

Can I use HTML tag?

Yes. Previously, HTML tag was used to show HTML tags on a webpage.

However, the plaintext HTML tag is deprecated since HTML version 2. That means they were remove from the official HTML language and may not work as expected. My advice: don’t use the HTML tag.

How Do I Display HTML Tags as Text in PHP?

If you want to use PHP to show HTML tags as text, then, you can use the function htmlspecialchars[] to escape < and > characters.

  htmlspecialchars['This shows HTML tags']

Now go out there and share the code. If you have any question in HTML, ask in the comments below.

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.

How do I display HTML tags as plain text in HTML?

Basically, there are two methods for displaying HTML tags as plain text..
Using element: Plaintext element is deprecated which means this feature is no longer supported. ... .
HTML entities: The second and only option available is using html entities. < ,>.

How do I convert HTML to text?

Below are several methods for converting, or saving, an HTML web page as a text document..
Click the File tab again, then click the Save as option..
In the Save as type drop-down list, select the Plain Text [*. txt] option. ... .
Click the Save button to save as a text document..

Why is my HTML code showing up as text?

It is possible that the content is not html type. I made a similar mistake and was wondering why it is showing as text. Check the content of the file, most probably it is not HTML or some html tags must be missing. Make sure that Doc type is explicitly mentioned as HTML type at the begining of the document.

How do I print HTML code in HTML?

You can easily add a print button to your web page by adding the following code to your HTML document where you want the button to appear:.
title="window.print[];return false;" />.
print..
type="text/css" media="print" />.
body {visibility:hidden;} .print {visibility:visible;}.

Chủ Đề