Change paragraph text dynamically with textarea by

2019-07-24 15:47发布

问题:

I want to display written textarea text in another textarea by clicking a third element. So here's the code I'm using at the moment but it shows the written text when you press the textarea:

  $(function(){
    $('#text').click(function(){
      $('#preview').text($(this).val());
    });
  });

And here's the HTML part:

<textarea id="text"></textarea>
<textarea id="preview"></textarea>

<div id="show-text"></div>

So the idea is to display the text inside textarea#text in textarea#preview when you click div#show-text.

回答1:

Your problem is that your click event is registered on the textarea. What you're actually looking for is:

  $(function(){
     $('#show-text').click(function(){
       $('#preview').text($('#text').val());
     });
   });