How do I insert new line characters to a string when entered in an input box [duplicate]












0
















This question already has an answer here:




  • HTML newline using fromCharCode is not working

    2 answers



  • html label inserting newline

    3 answers




I'm trying to replace substrings. The substitution is entered in an input box. The thing is that I am unable to insert n via the input box.



So this does not work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, replacement + '$&');


But this does work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, "n" + '$&');


Is there a way to accomplish this?










share|improve this question













marked as duplicate by T.J. Crowder javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 15:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

    – Wiktor Stribiżew
    Nov 28 '18 at 15:02











  • input type="text" is for single line text. textarea is what you want.

    – Daniel A. White
    Nov 28 '18 at 15:04











  • If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

    – T.J. Crowder
    Nov 28 '18 at 15:05











  • @Wiktor That almost worked but. It does insert a new line but also quotation marks

    – MK01111000
    Nov 28 '18 at 15:10











  • Oh, you must have pasted my code in a wrong way.

    – Wiktor Stribiżew
    Nov 28 '18 at 15:11
















0
















This question already has an answer here:




  • HTML newline using fromCharCode is not working

    2 answers



  • html label inserting newline

    3 answers




I'm trying to replace substrings. The substitution is entered in an input box. The thing is that I am unable to insert n via the input box.



So this does not work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, replacement + '$&');


But this does work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, "n" + '$&');


Is there a way to accomplish this?










share|improve this question













marked as duplicate by T.J. Crowder javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 15:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

    – Wiktor Stribiżew
    Nov 28 '18 at 15:02











  • input type="text" is for single line text. textarea is what you want.

    – Daniel A. White
    Nov 28 '18 at 15:04











  • If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

    – T.J. Crowder
    Nov 28 '18 at 15:05











  • @Wiktor That almost worked but. It does insert a new line but also quotation marks

    – MK01111000
    Nov 28 '18 at 15:10











  • Oh, you must have pasted my code in a wrong way.

    – Wiktor Stribiżew
    Nov 28 '18 at 15:11














0












0








0









This question already has an answer here:




  • HTML newline using fromCharCode is not working

    2 answers



  • html label inserting newline

    3 answers




I'm trying to replace substrings. The substitution is entered in an input box. The thing is that I am unable to insert n via the input box.



So this does not work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, replacement + '$&');


But this does work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, "n" + '$&');


Is there a way to accomplish this?










share|improve this question















This question already has an answer here:




  • HTML newline using fromCharCode is not working

    2 answers



  • html label inserting newline

    3 answers




I'm trying to replace substrings. The substitution is entered in an input box. The thing is that I am unable to insert n via the input box.



So this does not work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, replacement + '$&');


But this does work



var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, "n" + '$&');


Is there a way to accomplish this?





This question already has an answer here:




  • HTML newline using fromCharCode is not working

    2 answers



  • html label inserting newline

    3 answers








javascript regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 14:59









MK01111000MK01111000

896




896




marked as duplicate by T.J. Crowder javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 15:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by T.J. Crowder javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 15:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

    – Wiktor Stribiżew
    Nov 28 '18 at 15:02











  • input type="text" is for single line text. textarea is what you want.

    – Daniel A. White
    Nov 28 '18 at 15:04











  • If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

    – T.J. Crowder
    Nov 28 '18 at 15:05











  • @Wiktor That almost worked but. It does insert a new line but also quotation marks

    – MK01111000
    Nov 28 '18 at 15:10











  • Oh, you must have pasted my code in a wrong way.

    – Wiktor Stribiżew
    Nov 28 '18 at 15:11



















  • You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

    – Wiktor Stribiżew
    Nov 28 '18 at 15:02











  • input type="text" is for single line text. textarea is what you want.

    – Daniel A. White
    Nov 28 '18 at 15:04











  • If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

    – T.J. Crowder
    Nov 28 '18 at 15:05











  • @Wiktor That almost worked but. It does insert a new line but also quotation marks

    – MK01111000
    Nov 28 '18 at 15:10











  • Oh, you must have pasted my code in a wrong way.

    – Wiktor Stribiżew
    Nov 28 '18 at 15:11

















You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

– Wiktor Stribiżew
Nov 28 '18 at 15:02





You need to define a kind of a special newline representation. If you are OK with n, just use replacement.replace(/\n/g, "n")

– Wiktor Stribiżew
Nov 28 '18 at 15:02













input type="text" is for single line text. textarea is what you want.

– Daniel A. White
Nov 28 '18 at 15:04





input type="text" is for single line text. textarea is what you want.

– Daniel A. White
Nov 28 '18 at 15:04













If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

– T.J. Crowder
Nov 28 '18 at 15:05





If the linked questions don't solve your problem, your question may not be clear. If that's the case, please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one).

– T.J. Crowder
Nov 28 '18 at 15:05













@Wiktor That almost worked but. It does insert a new line but also quotation marks

– MK01111000
Nov 28 '18 at 15:10





@Wiktor That almost worked but. It does insert a new line but also quotation marks

– MK01111000
Nov 28 '18 at 15:10













Oh, you must have pasted my code in a wrong way.

– Wiktor Stribiżew
Nov 28 '18 at 15:11





Oh, you must have pasted my code in a wrong way.

– Wiktor Stribiżew
Nov 28 '18 at 15:11












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes