jQuery messes up line breaks

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


jQuery messes up line breaks



I've got an if else statement. Switching between a text in <p> tag and the same text in an editable <textarea>


<p>


<textarea>



The problem is that:



you press the edit button and script converts p tag to textarea. Text is converted fine with line breaks.



You press save. Text is converted correctly back into a p tag again.



You try to press edit again... but this time, the linebreaks are ignored!?


var curState = "Ret";

if (curState == "Gem"){
curState = "Ret";

var p = btn.closest("div").find("textarea");

var t = jQuery(this).closest("div").children("div").children('textarea').val().replace(/(rn|n)/g, "<br/>");

var ta = jQuery("<p/>", {
"html": t
});
p.replaceWith(ta);
}else{
curState = "Gem";

var p = jQuery(this).closest("div").find("p");

var t = p.text()

var ta = jQuery("<textarea/>", {
"class": "editTextarea",
"val": t
});
p.replaceWith(ta);
}





Don't use html method on a textarea.... set it's value with val(). Beyond that it appears you are replacing all the line breaks so it is expected it would be different second time. Please provide a runnable Minimal, Complete, and Verifiable example. Click on <> in question editor to create a snippet that will run in this page
– charlietfl
11 mins ago




<>





Hint: try storing original text that includes n in data() of the <p> instead of relying on it's textContent
– charlietfl
8 mins ago




n


data()


<p>









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty