Does anyone know a simpler way to do this effect, getting the text instantly when typing in an input

Asked

Viewed 79 times

2

<html>
<head>
  <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <title>Teste</title>
</head>
<body>
  <div id="formVal">
<form name="fvalida">
  <input type="text" id="telp" placeholder="">
</form>
<div id="preview_form">
  <div id="telp1"></div>
</div>
  <script>try {jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
  this.each(function() {
    {; for (var x=1000; x<=intShakes; x++) {;
    }}
  });
};

$("#telp").keyup(function(){
  $("#telp1").html($(this).val());
});

} catch (error) { throw error; }

</script>
</body>
</html>
  • 1

    What should that code do? What do you want it to do?

  • type into an input or textarea and appear in a div instantly.

1 answer

2


Just use

$("#telp").keyup(function() {
  $("#telp1").html(this.value);
});

If you want to do only with native Javascript you can do so:

var telp = document.getElementById('telp');
var telp1 = document.getElementById('telp1');
telp.addEventListener('input', function() {
  telp1.textContent = this.value;
});
<form name="fvalida">
  <input type="text" id="telp" placeholder="">
</form>
<div id="preview_form">
  <div id="telp1"></div>
</div>

  • @Voltz its edition was rejected because the changes linguistic characteristics that in no way affect the interpretation of the post, besides being correct grammatically.

  • Very good! but there is still a part that I could not implement that is the breaking of the div when exceed a number of characters and move to line below, because when I type the div passes the note screen.

  • @Elvisdacunhapereira this is a CSS problem. You can solve it in different ways, for example with #telp1{word-wrap: break-word;}. But that’s a different question, if you can’t find an answer you can ask a question about it.

  • It’s true!! had not attacked me to that.

Browser other questions tagged

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