Home >Web Front-end >JS Tutorial >jQuery method to replace line breaks in textarea_jquery
The example in this article describes how jQuery replaces line breaks in textarea. Share it with everyone for your reference. The specific analysis is as follows:
When my colleague was learning jQuery yesterday, he wanted to replace the line breaks in the textarea.
html part:
<fieldset> <textarea id="ncontent"></textarea> </fieldset> <button id="submit">提 交</button>
js part:
$(document).ready(function(){ $("#submit").click(function(){ var ss = $("#ncontent").text(); alert(ss); var str = ss.replace(//r/n/gi, "!!!"); alert(str); }); }); </script>
After using JavaScript’s replace() method, the newline is still not replaced.
The problem is that jQuery gets the value in textarea. Should use
Using the text() or val() method will process the html tags in the textarea, so /r/n will definitely not be replaced.
I hope this article will be helpful to everyone’s jQuery programming.