1

So I have the following in my page which is a textarea with some text in it:

enter image description here

and what i want to do now is to replace the BR in the text with newline \r such that i would get the following displayed in the textarea:

Hi
I
Am
Jake

Here's what I tried so far:

console.log(document.getElementById("sample").value);
document.getElementById("sample").value = document.getElementById("sample").value.replace("<BR>","\n");
html, body {
    height: 100%;
}

textarea {
    resize: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" href = "style.css">
    <title>Document</title>
</head>
<body>
    <div id = "outer">
        <textarea id="sample">Hi<BR>I<BR>Am<BR>Jake</textarea>
    </div>

    <script src="test.js"></script>
</body>
</html>

However, it doesn't seem to be displaying properly. Would appreciate some help on this.

4 Answers 4

1

Use replaceAll() instead of replace(). The replace function will replace only the first match, replaceAll() will replace all the occurrences in the given string.

I also suggest escaped strings in the replace parameters. Add backslash (\) in your < > characters.

console.log(document.getElementById("sample").value);
document.getElementById("sample").value = document.getElementById("sample").value.replaceAll("\<BR\>","\n");
html, body {
    height: 100%;
}

textarea {
    resize: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" href = "style.css">
    <title>Document</title>
</head>
<body>
    <div id = "outer">
        <textarea id="sample">Hi<BR>I<BR>Am<BR>Jake</textarea>
    </div>

    <script src="test.js"></script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

@CodeBug - So does your comment. ;)
@enhzflep LOL :-D
0

JavaScript's replace function only replaces the first instance. You can, however, use regex with the /g flag. I replaced "<BR>" with /<BR>/g, and it works (you just have to scroll). If you're interested, there is also String.prototype.replaceAll, but that doesn't have that good browser support.

console.log(document.getElementById("sample").value);
document.getElementById("sample").value = document.getElementById("sample").value.replace(/<BR>/g,"\n");
html, body {
    height: 100%;
}

textarea {
    resize: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" href = "style.css">
    <title>Document</title>
</head>
<body>
    <div id = "outer">
        <textarea id="sample">Hi<BR>I<BR>Am<BR>Jake</textarea>
    </div>

    <script src="test.js"></script>
</body>
</html>

Comments

0

Use .value.split("<BR>").join("\n") instead of .value.replace("<BR>", "\n")

document.getElementById("sample").value = document.getElementById("sample").value.split("<BR>").join("\n");
html, body {
    height: 100%;
}

textarea {
    resize: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" href = "style.css">
    <title>Document</title>
</head>
<body>
    <div id = "outer">
        <textarea id="sample">Hi<BR>I<BR>Am<BR>Jake</textarea>
    </div>

    <script src="test.js"></script>
</body>
</html>

Comments

0

Here is my solution:

document.getElementById("sample").value = document.getElementById("sample").value.replace(/\<BR\>/g,"\n").trim();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.