Search code examples
javascriptsafari

Why does the ↵ enter symbol appear in an array string? in Safari


I have Javascript project with element.addEventListener('click', function(){}), so when it clicked that element.innerText is pushed to array (at this stage strings works correct on Safari - like 'Name1').

And it works fine anywhere besides Safari and iOS browsers. I noticed in Safari console that array looks like ["↵Name1↵","↵Name2↵","↵Name3↵"] and in other browsers it looks like ["Name1","Name2","Name3"] which is correct.

Does anybody know why this ↵ enter symbol came out and how to remove it?


Solution

  • Instead of

    const text = element.innerText;
    array.push(text);
    

    use

    const text = element.textContent.trim();
    array.push(text);
    

    See: