cdub
cdub

Reputation: 25701

Using JQuery Key press to grab textarea data and display in a span

I want to grab what a user is typing and display in a span above the textarea. But how do I grab the enter/return key (keyCode 13), and insert it correctly into the span so that a line break in the textarea, is a line break (
) in the span.

 $('#InviteMessage').keyup(function(event)
    {
        var enter = "";

        //if(event.keyCode = '13')
            //enter = 'br />';

        var text = $(this).val() + enter;

        //replace all the less than/greater than characters

        if(text == '')
            $('#message').html('[Your personal message]');
        else
            $('#message').html(text);
        }
    );

The #InputMessage is the textarea and #message is the span above it.

Upvotes: 1

Views: 989

Answers (1)

alex
alex

Reputation: 490233

You could simply do...

text = text.replace(/\n/g, '<br />');

Or use white-space: pre on the span, in which case the span should probably be a div.

Upvotes: 4

Related Questions