How to insert text into specific line in textarea with jQuery?

lol

If I have this HTML code:

<textarea>
line 1 text
line 2 text
line 3 text
line 4 text
line 5 text
</textarea>

How can I insert text after the third line, so my result is this:

<textarea>
line 1 text
line 2 text
line 3 text
this is my new text here!!!!!!!!!
line 4 text
line 5 text
</textarea>
zfrisch

We create a function that takes your text and a line number, splits the current text within the box by the new line character, add in the new text, and update the textarea box.

function addLine(text, line) {
  let ta = document.querySelector("textarea"), 
  split_text = ta.textContent.split(/\n/gmi);

    if (line <= split_text.length - 1) split_text.splice(line - 1, 0, text);
    else split_text.push(text);
  ta.textContent = split_text.join("\n");
}

addLine("This is my new text here!", 3);
addLine("This is another example!", 7);
addLine("And one more!", 2);

function addLine(text, line) {
let ta = document.querySelector("textarea"), 
split_text = ta.textContent.split(/\n/gmi);

  if (line <= split_text.length - 1) split_text.splice(line - 1, 0, text);
  else split_text.push(text);
  ta.textContent = split_text.join("\n");
}

addLine("This is my new text here!", 3);
addLine("This is another example!", 7);
addLine("And one more!", 2);
textarea {
  width: 300px;
  height: 300px;
}
<textarea>
line 1 text
line 2 text
line 3 text
line 4 text
line 5 text</textarea>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Insert text into textarea with jQuery

How do I insert text at specific line numbers in a text file?

Insert text at specific line number

How can I insert a text file at specific line of another

how to insert a line text after specific number of lines

Keep line breaks with jQuery .text() in textarea

JavaFX: How to scroll to a specific line in the textarea

Insert text into textarea and help text when mouseover buttons with jQuery/Javascript

sed mac insert tab and text at specific line

How to push a line of text down in a textarea box?

How can I show a specific text on a textarea?

How To Select A Specific text In A TextArea In Swing

How can I insert a line of text above the occurence of a specific text in a long R Script?

Jquery Append or Insert Text After Specific Words

sed: insert text after specific character in a specific line

insert variable text after another variable text on a specific line

SED Insert text after a specific multi-line text field

Find specific text, in line above this text insert characters at set positions

How to capture TextArea text changed event in JQuery

Extracting specific text from textarea between two words via jquery

How to insert HTML text to jQuery

How to insert output of a command at a specific line?

How to insert a line break in specific anchor in JS?

how to insert line break after a specific string?

jQuery each line in textarea

How to insert text into one textarea out of several with id?

How to insert text before the first line of a file?

How to insert text at line and column position in a file?

vim how to cut text and insert into a new line