Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show text while typing

Tags:

jquery

I thought this one was a simple one but I wasn't able to find anything out there, except one post here on STO.

Problem is the code doesn't work. I created a fiddle so you can see it for yourself.

Here is the code from the fiddle:

$('#someTextBox').keyup(function() {
    $('#target').html(this.val());
});

However, my HTML is a bit different than the example:

<textarea name="comment-box" id="comment-box" class="required"></textarea>
...
<p id="comment-preview"></p>

All I need help with is a way to display what's being typed on the textarea on the "comment-preview" container.

Any help guiding me on this one is greatly appreciated.

like image 690
Ricardo Zea Avatar asked Nov 05 '12 20:11

Ricardo Zea


4 Answers

Change this.val() to $(this).val()

DEMO: http://jsfiddle.net/FjNzS/1/

.val is a jQuery function and can be accessed from jQuery object. Inside the handler, this is DOM object and so you need to wrap it with $() to make it a jQuery object.

like image 186
Selvakumar Arumugam Avatar answered Nov 07 '22 14:11

Selvakumar Arumugam


You can either use $(this).val() or this.value, but this.val() is incorrect.

like image 5
elclanrs Avatar answered Nov 07 '22 14:11

elclanrs


You can also try this code with .on():

$('#someTextBox').on('keyup', function(){
    $('#target').html($(this).val());
}); 

Exemple http://jsfiddle.net/FjNzS/2/

like image 2
RDK Avatar answered Nov 07 '22 15:11

RDK


$("#comment-box").keyup(function() {
    $("#comment-preview").text($(this).val());
});
like image 1
Adam Moss Avatar answered Nov 07 '22 14:11

Adam Moss