Home > Web Front-end > JS Tutorial > jQuery method to replace line breaks in textarea_jquery

jQuery method to replace line breaks in textarea_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:55:56
Original
1482 people have browsed it

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> 

Copy after login

js part:

$(document).ready(function(){ 
  $("#submit").click(function(){ 
    var ss = $("#ncontent").text(); 
    alert(ss); 
    var str = ss.replace(//r/n/gi, "!!!"); 
    alert(str); 
  }); 
}); 
</script>
Copy after login

After using JavaScript’s replace() method, the newline is still not replaced.

The problem is that jQuery gets the value in textarea. Should use

Copy code The code is as follows:
var ss = $("#ncontent").html();

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.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template