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?
            Asked
            
        
        
            Active
            
        
            Viewed 48 times
        
    2
            
            
        
        DonFuchs
        
- 133
 - 7
 
- 
                    you can get your answer from this SO https://stackoverflow.com/questions/10376179/n-t-r-0-is-true" Link – Pushprajsinh Chudasama Aug 31 '19 at 07:02
 - 
                    Thanks, thus it is `+"\n" == +"\r"` for example. But then it seems `indexOf` is comparing characters/strings as numbers... although it is looking for a string in a string?! – DonFuchs Aug 31 '19 at 07:34
 - 
                    Because we still have `"\n" != "\r"` – DonFuchs Aug 31 '19 at 07:36
 
1 Answers
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
});