Reputation: 1530
How to make Paste Button Working?
I have 1 element textarea for My Text.
<textarea id="myText">Ananda</textarea>
I want to Paste it into another input text .
<input type="text" id="resultPaste">
I have button for Paste from #myText
to #resultPaste
.
<span class="btn-main btn-paste">PASTE</span>
My code jQuery :
$(document).ready(function() {
$(".btn-paste").click(function() {
var text = $('#myText').val()
$('#resultPaste').append(text);
});
});
Code Snippet Demonstration:
$(".btn-paste").click(function() {
var text = $('#myText').val();
$('#resultPaste').append(text);
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<textarea id="myText">Ananda</textarea>
<br>
<button class="btn-primary btn-paste">PASTE</button>
<br><br>
<input type="text" id="resultPaste">
Upvotes: 2
Views: 675
Reputation: 11819
I don't know what append does, but in order to set the value, you must use
el.val('something')
$(".btn-paste").click(function() {
var text = $('#myText').val();
$('#resultPaste').val(text);
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<textarea id="myText">Ananda</textarea>
<br>
<button class="btn-primary btn-paste">PASTE</button>
<br><br>
<input type="text" id="resultPaste">
Upvotes: 3
Reputation: 64
$(document).ready(function() {
$(".btn-primary").click(function() {
var text = $('#myText').val();
$('#resultPaste').val( $('#resultPaste').val()+text);
});
});
append dont work on textarea
Upvotes: 5
Reputation: 262
No jquery
window.addEventListener("DOMContentLoaded", () => {
document.getElementsByClassName("btn-paste")[0].addEventListener("click", () => {
document.getElementById("resultPaste").value = document.getElementById("myText").value;
});
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<textarea id="myText">Ananda</textarea>
<br>
<button class="btn-primary btn-paste">PASTE</button>
<br><br>
<input type="text" id="resultPaste"/>
Upvotes: -1
Reputation: 10945
Since you are just copying all of the text over just use .val()
on both the copy <textarea>
and the past <textarea>
.
$(".btn-paste").click(function() {
$('#resultPaste').val($('#myText').val());
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<textarea id="myText">Ananda</textarea>
<br>
<button class="btn-primary btn-paste">PASTE</button>
<br><br>
<input type="text" id="resultPaste">
Upvotes: 1