View mysql html data with Javascript

Asked

Viewed 321 times

1

I have HTML data recorded in the database and I want to show it on the screen, but when I show this data instead of formatting it as html, the browser displays as text the tags and html code coming from the database.

How do I display it without being a text but yes, format it as html?

        <!-- Post Content -->
        <section>
          <p><%= row.editor1 %></p>
        </section>

How are you displaying: inserir a descrição da imagem aqui

  • In the print shown in the question there seems to be no HTML code coming in the variable row.editor1. The only tag you have in the print is the <p> and it’s already in HTML.

1 answer

0


One way to accomplish what you asked would be by using the property Element.innerHTML, in accordance with the documentation:

The Element.innerHTML property defines or obtains the HTML syntax describing the descending elements.

A very simple example of the use would be the following:

HTML code

<html lang="pt-br">
<head>
  <title>Exemplo de Alteração de Html usando Javascript</title>
</head>
<body>
<div>
<section id="areaConteudo" name="areaConteudo">

</section>
</div>
</body>
</html>

Javascript code

var conteudoHtml="<h1>Conteúdo adicionado pelo JavaScript</h1><br><br><p>Perceba que as tags HTML são corretamente interpretadas</p>";
document.getElementById("areaConteudo").innerHTML=conteudoHtml;

The result of which is as follows:: Resultado do código do exemplo

See working in Jsfiddle.

Basically, you are setting the "child" content that will be presented within the Section.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.