Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change paragraph text dynamically with jQuery?

I want to take the information typed into a text field and display it in a paragraph elsewhere on the page. Basically, exactly what is happening below as I'm type this (go to post a question and start typing in the main text box and you'll see what I mean).

Any idea how to do this? The page has so much JavaScript on it I can't find how they did it.

like image 587
Carson Avatar asked Aug 09 '10 15:08

Carson


People also ask

How do you change the text in a paragraph dynamically?

keyup(function(){ $('#preview'). text($(this). val()); }); }); This grabs the value of textarea on its keyup event and later the paragraph's text is changed ( text() method) with that of textarea $(this).

How do I change the value of a paragraph in jQuery?

To change text inside an element using jQuery, use the text() method.


1 Answers

I think you are looking for this.

Here is how your html should look like:

<textarea id="txt" style="width:600px; height:200px;"></textarea> <p id="preview"></p> 

And jQuery:

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

This grabs the value of textarea on its keyup event and later the paragraph's text is changed (text() method) with that of textarea $(this).val().

like image 150
Sarfraz Avatar answered Sep 19 '22 12:09

Sarfraz