Receive code from input

Asked

Viewed 40 times

2

How do I treat input text as a string even if they are html tags?

Ex: if I type

<b>texto</b>

in an untreated input field it appears like this text, but I want it written anyway:

<b>texto</b>
  • You’re doing it with pure javascript or jquery?

  • jQuery for the most part, but the easiest thing for you will already help me.

1 answer

4


In pure javascript you can do so:

texto.onkeyup = function() {
  
  resultado.textContent = this.value
    
}
<input type="text" id="texto">


<div id="resultado">
  
  
</div>

In jQuery:

$('#texto').keyup(function() {
  $('#resultado').text($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="texto">

<div id="resultado"></div>

  • legal, simple and practical. In this case the secret is in textContent?

  • 1

    Exactly, instead of using innerHTML, uses textContent, which ignores the interpretation of tags, put an example with jquery also!

  • Perfect, great fight!

Browser other questions tagged

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