简体   繁体   中英

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?

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>

Try using keyup like this:-

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

</script>

try .Change() event

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

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM