I am inserting a json string value as a value of a button element attribute, like for example below
var $json = JSON.stringify('{"Long_text":"This is \'my json string and soon..."}');
$("#button_id").attr('data-json', $json);
This works in some of my pages but when there is a single quote in the text even if it is escape with a slash the value in the element attribute creates newline and it breaks like:
<button data-json="{"Long_text":"This is \' "
"my json string and soon..."}" >Click</button>
I have tried using
replace('/\r?\n|\r|\n/g/',''); //to replace multiple newlines
Even if I replace the double spaces it doesn't work because the attribute itself was malformed. So when I get the attribute and try to parse the json value it cause an error.
I have found this,"->Line break inside HTML tag attribute value" should I replace the spaces with this %0D%0A ? as suggested to preserved newlines or spaces?
Any help or advise is well appreciated! Thanks!