M
M
Mari Kozak2020-12-16 15:57:56
JavaScript
Mari Kozak, 2020-12-16 15:57:56

How to use the enter key in an array?

js question

when you enter into input:
coding
day bolcshe texta
dlya primera
ti samiy lychiy
zadacha ogon

from the input comes to me, like this:
coding day bolcshe texta dlya primera govno spasibo Vladimir ti samiy lychiy zadacha ogon

I need to work with such an array and iterate over it:
"coding"
"day bolcshe texta"
"dlya primera"
"ti samiy lychiy"
"zadacha ogon"

the problem is that enter serves as a form submission, that is, if you press enter everything goes wrong, the value will be erased and will not be displayed. enter doesn't work as a delimiter. Now I made it so that enter is simply ignored.
$(".comment-input input").on("keypress", function (event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});

and I need, when the user enters enter, I thought that this is the next element of the array, and not a new word in it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dexdot, 2020-12-17
@vmlzn

In order for the field to be able to separate the text on new lines, input should be replaced. Next, to get an array of values, you can use the following:
<textarea></textarea>

const value = document.querySelector('textarea').value;
const splittedValue = value.split(/\n/);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question