Line breaking

Asked

Viewed 160 times

-1

Hi, I created a chat on my website using Firebase. But I don’t know how to make this message be saved along with line breaking.

imagem 1

In the image above I am using the textarea to write the message.

More after sending the message she does not do the line break as shown in the photo below.

image 2

This is my code that saves the message in the Firebase database.


    $("#send_button").on('click', function () {

        var user = firebase.auth().currentUser;
        var displayName = user.displayName;
        var photoURL = user.photoURL;
        var mess = $("#msg").val();
        var date = formatAMPM(new Date());

        firebase.database().ref('chat/' + Date.now()).set({
            name: displayName,
            photo: photoURL,
            message: mess,
            date_and_time: date
        });
        var element = document.getElementById("scroll-chat");
        element.scrollTop = element.scrollHeight;
        $("#msg").val("");

    });

1 answer

1


I believe the problem is in the view. Textarea sends break line with \n and html does not read it.

Try changing your string to <br />, something like that:

var str = 'olá\nEste é um teste de quebra de linha, teste1.';
str.replace(/\n/g,'<br />');
  • I was able to solve the problem by changing the variable to var mess = $("#msg"). val(). replace(/ r? n/g,'<br/>');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.