peped
peped

Reputation: 11

Change paragraph text dynamically with textarea by clicking third element with jQuery?

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.

Upvotes: 1

Views: 1737

Answers (2)

Bogdan Bibina
Bogdan Bibina

Reputation: 120

I made a function in Javascript,that is adding data into the paragraph:

  <p id="textToAdd"></p>
    Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
    
    <script>
    function myFunction() {
        $('#textToAdd').innerHTML = $('#fname').value;
       
    }
    </script>

Upvotes: 0

Abraham P
Abraham P

Reputation: 15471

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());
     });
   });

Upvotes: 6

Related Questions