0

Is there a way to convert HTML text to plain text in JS WITHOUT IMPORTING OR USING 'NPM INSTALL'. I am trying to, for example, put: <h1>Hello</h1> as <h1>Hello</h1> without it autocorrecting to

'Hello'.

Is this possible? Even if it can put it in blockquotes or multiple line blockquotes like

<h1>Hello</h1>
<h2>Hi</h2>

How do I do this does anyone know?

I would also be fine with figuring out how StackOverflow creates their blockquotes like:

<h2>Hello</h2>

Can someone help please!

  • 1
    HTML is plain text until you put it in a browser. Where is the HTML string coming from? Are you selecting a node? What are you doing with your converted string? What have you tried so far? It isn't obvious from your example what your are trying to achieve. – Jon P Jul 12 '21 at 03:47
  • https://stackoverflow.com/questions/6817262/how-to-display-html-tags-as-plain-text - I think this is your solution. – Nguyen Hoang Jul 12 '21 at 03:50
  • Why would a string with an HTML `h1` element and the content `Hello` be rendered as `'Hello'.`? It is not clear where the single quotes come from. Other than that I agree with the first comment, in what context do you use JavaScript and that string? Only if fed to the browser will it be rendered as a heading with a certain font-size and as bold text. But that all depends on how you fed it to the browser. If you use `var code = document.createElement('code'); code.textContent = '

    Hello

    '; document.body.appendChildcode);` you will see the HTML code.
    – Martin Honnen Jul 12 '21 at 07:38
  • @JonP the HTML is coming from a predefined array its hard to understand if you have never used https://qoom.io/ I am using a database that Qoom has. @NguyenHoang sorry thanks for the help but Qoom doesn't support PHP. @MartinHonnen I put the quotes as like it shows `Hello`. And I dont want to create an element I already have predefined elements. – App Developer Jul 12 '21 at 15:29

2 Answers2

0

Using getElementById you can select any dom element. The dom element has outerHTML property. I think you can use this property.

<html>

<div id="test">
<h1>Hello</h1>
<h2>Hi</h2>
</div>

</html>

in js file

a = document.getElementById("test").outerHTML;
console.log(a + 'hello');

working fiddle - https://jsfiddle.net/alimurrazi/gp05cytr/7/

Alimur Razi Rana
  • 338
  • 3
  • 15
  • sorry this doesn't help... This is pre-defining the code in the HTML but I want to use a system to input it in through JS. – App Developer Jul 12 '21 at 04:07
  • I don't understand your requirement. What do you want? You have a HTML elelemt like -

    . And from js you populate the HTML element -

    Hello

    . Does it match with your question?
    – Alimur Razi Rana Jul 12 '21 at 05:00
  • You see how in stack overflow you can write h1 in the blockquote without it actually creating an h1 and displaying in GIANT text... I am trying to mimic that system. – App Developer Jul 12 '21 at 15:33
0

This is a non-deprecated deprecated tag. <xmp>

<xmp> is not an easy-to-find tag... I would recommend using it!

It says its deprecated if you search it online... but after using it in a live display it is definitely a working tag!

  • Deprecated doesn't mean not working, it means it is not guaranteed to work now or in the future. I would *not* recommend using it. The string you are working with should be HTML encoded if you wish to display the code, ideally from the server end, if that is not possible [this answer](https://stackoverflow.com/a/18750001/4665) may help. The `xmp` tag is absolutely deprecated. – Jon P Jul 12 '21 at 23:42
  • you could also just use tags @JonP – App Developer Jul 22 '21 at 03:22