Skip to content Skip to sidebar Skip to footer

How Can I Escape A \n From An Html-attribute To A Js-string?

I'm trying to pass new-lines from a html-attribute to a javascriptstring, in order to apply it to a textarea:
var parsed_string = string.replace(/\\n/g, "\n");

You might be better off using an existing language, like JSON, which would give you more options in the future and come with prebuilt parsers.

var mode = "mode2";
var string = $('#check').data(mode);
var parsed_string = JSON.parse(string);
$('textarea').val(parsed_string);
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label><inputid="check"type="checkbox"data-mode2="&quot;Varför är det så mycket administration! \n\n&quot;">Mer tid att lägga på att träffa barn, istället för administration</label><textarea></textarea>

Note that I had to wrap the data in " characters to make it a valid JSON string.

Post a Comment for "How Can I Escape A \n From An Html-attribute To A Js-string?"