javascript - Remove line-breaks and spaces from textarea - Stack Overflow

<textarea rows='5' cols='50' id='content'><textarea><inp

<textarea rows='5' cols='50' id='content'></textarea>
<input type='button' value='Extract Text' onclick='someFunction()'/>

Assume these two html elements. My problem is the following:

Let's say that the user enters into the textarea field something like this, exactly as typed. With all line breaks and spaces. (And yes, this is Python :P)

if (...):  
   print "Hello everyone!"
else:
   print "Dudes help me out!"

My objective is to make a JavaScript function which removes all the spaces and line breaks (typed with 'Enter') and returns a space-less string. Something like "ThisIsASampleText". I've used document.getElementById('content').value and myString.replace(/\s+/g, '') to remove spaces. Can't make it work. Any ideas?

Best Regards

<textarea rows='5' cols='50' id='content'></textarea>
<input type='button' value='Extract Text' onclick='someFunction()'/>

Assume these two html elements. My problem is the following:

Let's say that the user enters into the textarea field something like this, exactly as typed. With all line breaks and spaces. (And yes, this is Python :P)

if (...):  
   print "Hello everyone!"
else:
   print "Dudes help me out!"

My objective is to make a JavaScript function which removes all the spaces and line breaks (typed with 'Enter') and returns a space-less string. Something like "ThisIsASampleText". I've used document.getElementById('content').value and myString.replace(/\s+/g, '') to remove spaces. Can't make it work. Any ideas?

Best Regards

Share Improve this question edited Dec 16, 2019 at 12:22 vsync 131k59 gold badges340 silver badges423 bronze badges asked Aug 16, 2015 at 20:42 incaliteincalite 3,2173 gold badges28 silver badges32 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

Here's a live demo of how to use the regex you described to remove all whitespace in the text from your textarea:

function someFunction() {
    var output = document.getElementById("content").value;
    output = output.replace(/\s/g, "") 
    document.getElementById("output").innerHTML = output;
}
<textarea rows='5' cols='50' id='content'></textarea>
<input type='button' value='Extract Text' onclick='someFunction()'/>
<div id="output"></div>

JSFiddle Version: https://jsfiddle/afy5v66x/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743948560a4534518.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信