2

If I set the value of a textarea to let's say "123456\r123" and call document.getElementById('myTextarea').value.indexOf("\n"), it returns 6, which is the position of the line break; but why is it not returning -1, as there is no \n inside the string?

DonFuchs
  • 133
  • 7

1 Answers1

0

I guess the browser auto transfer the '\r' to '\n' when you set value to the textarea. When I run those codes in browser,the result told me what I guess is just the truth.

let textArea = document.getElementById("inputTextarea");

let value = "abcd\radc";

textArea.value = value;

let out = document.getElementById("outputPre");

document.getElementById("inputTextarea").addEventListener("click", () => {
    value = textArea.value;

    let chars = value.split("").map(s => s.charCodeAt(0));

    let lf = ["\r".charCodeAt(0), "\n".charCodeAt(0)];

    out.innerHTML = 'valueChars:<em>' + value.split("") + "</em><br/>ASCII:<em>" + chars.toString() + "</em>";
    /*valueChars:a,b,c,d,
    ,a,d,c ASCII:97,98,99,100,10,97,100,99*/

    console.info(lf); //13,10
});

DonFuchs
  • 133
  • 7
Chivenh
  • 114
  • 4