Mustafa
Mustafa

Reputation: 996

How to I get a <textarea> value on the fly using jQuery?

I'm using jQuery to retrieve and process a <textarea> content and process it on the fly. For example:

<textarea id='a'></textarea>
<textarea id='b'></textarea>
<script>
    $('#a').keypress(function() {
        $('#b').text(this.value);
    });

</script>

The problem with this is that it seems that the event fires before the element value changes. Thus, I'm always one character behind.

Is there a way to get the value as it changes?

Upvotes: 0

Views: 199

Answers (3)

xJoshWalker
xJoshWalker

Reputation: 457

From the documentation of jQuery: http://api.jquery.com/keyup/

The keyup event is sent to an element when the user releases a key on the keyboard.

<textarea id='a'></textarea>
<textarea id='b'></textarea>
<script>
    $('#a').keyup(function() {
        $('#b').text(this.value);
    });

</script>

Upvotes: 2

AlexT82
AlexT82

Reputation: 1124

try .Change() event

<script>
    $('#a').Change(function() {
        $('#b').text(this.value);
});

</script>

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

Try using keyup like this:-

<textarea id='a'></textarea>
<textarea id='b'></textarea>
<script>
    $('#a').keyup(function() {
        $('#b').text(this.value);
    });

</script>

Upvotes: 1

Related Questions