Ph Dev
Ph Dev

Reputation: 37

How to duplicate text every line in textarea using jquery

I want to duplicate text every line in textarea using jquery..

<!--
first, im gonna paste my text in input like this..

<textarea id="input">
one

two






three

four


five



six


seven


(and so on...)
</textarea>



and the result is like this..
<textarea id="Result">
one
one
two
two
two
two
two
two
two
three
three
four
four
four
five
five
five
five
six
six
six
seven
seven
seven
seven
</textarea>

duplicating text is depend on break line..
-->
here is my code:
<textarea id="input" style="width:100%; height:100px;" placeholder="input" ></textarea>
<textarea id="output" style="width:100%; height:100px;" placeholder="output" ></textarea>
<input type="button" value="Process!" />

I hope you can help me guys, thanks in advance..

Upvotes: 2

Views: 151

Answers (2)

NewToJS
NewToJS

Reputation: 2772

Since you have javsascript tagged in the question I thought I would give you a demo without using jQuery.

Interested in using this method please comment blow and i will explain anything you don't understand about this method.

window.onload=function(){
	var a=document.getElementById('input');
    a.addEventListener('input',MyFunction,false);
    a.focus();
}
function MyFunction(){
	var DataIn=event.target.value;
	DataIn = DataIn.replace(/(\n){1,}/g, '\n');
	document.getElementById('output').value=DataIn;
}
<textarea id="input" style="width:100%; height:100px;" placeholder="input" ></textarea>
<textarea id="output" style="width:100%; height:100px;" placeholder="output" ></textarea>

Update: jQuery method

$('#input').keyup(function(){
    $('#output').val($(this).val().replace(/(\n){1,}/g, '\n'));
});

I hope this helps. Happy coding!

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388326

You could do something like

var invalue = $('#input').val();
$('#output').val(invalue.replace(/^(.*)$/gm, '$1\n$1'))

Demo: Fiddle

Upvotes: 1

Related Questions