A
A
Andyy222020-10-13 13:24:26
Chats
Andyy22, 2020-10-13 13:24:26

Chat in JS - how to remove a line break when pressing Enter?

I want to make sure that when you press Enter, a message is sent, which happens, but also at the time of sending, it flips to a new line and then sends, can you tell me how to remove it?

function textAreaChat(event) {
    console.log(event.keyCode)
    var keyCode = event.keyCode || event.which;
    console.log(keyCode)
    if ((event.keyCode == 10 || event.keyCode == 13) && event.ctrlKey) return
    if (keyCode == 13) {
        var content = this.value;
        var caret = getCaret(this);
        console.log('cntl', event.ctrlKey)
        if (event.shiftKey) {
            this.value = content.substring(0, caret - 1) + "\n" + content.substring(caret, content.length);
            event.stopPropagation();
        } else if (keyCode == 13) {
            event.preventDefault();
            this.value = content.substring(0, caret - 1) + content.substring(caret, content.length);
            this.form.querySelector('button[type="submit"]').click();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question