Reputation: 117
I want to add text to a textarea when the user clicks a button. I know how to concatenate strings and add them to the textarea, what I want is to add text to a specific place/position inside textarea. When the user clicks the button it should add text in that position or replace the selection in the textarea.
<div>
<button onclick="addInplace('someText')">Add someText</button>
<textarea></textarea>
</div>
Upvotes: 3
Views: 35029
Reputation: 454
Sample example of HTML with JS that adds "someText" to the cursor position of the textarea, when you click the "Add someText" button.
<div>
<button onclick="addInplace('someText')">Add someText</button>
<textarea></textarea>
</div>
Activate the js function by clicking on your element
function addInplace(text){
//get the textarea either from function arg or in reference to event location
let textArea=event.target.closest("div").querySelector("textarea")
if (!text || !textArea) return;
navigator.clipboard.writeText(text) //optional add text to clipboard
textArea.focus()
let start=textArea.selectionStart
let end=textArea.selectionEnd
let val=textArea.value
textArea.value=val.slice(0,start)+text+val.slice(end,val.length)
}
Upvotes: 0
Reputation: 41
You need to add text at specific position (at caret position) into the text area when you click on a button. Try the following:
var position;
function getCaretPosition() {
var ctlTextArea = document.getElementById('textArea');
position = ctlTextArea.selectionStart;
return position;
}
/* Needs JQuery */
$(document).ready(function () {
jQuery.fn.extend({
insertAtCaret: function (myValue) {
return this.each(function (i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
})
}
});
$('#btnTest').click(function () {
$("#textArea").insertAtCaret(' << inserted text! >> ');
});
});
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<title>How to add text to textarea when user clicks a button</title>
</head>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>
// The javascript code goes here...
</script>
<body>
<h1>How to add text to textarea when user clicks a button</h1>
<div id="divDroppedFields">
<textarea id="textArea" name="txtMessageFields" class="required" cols="80" rows="10" onclick="getCaretPosition()" onkeyup="getCaretPosition()">
In the meantime the cat slowly recovered. The socket of the lost eye presented, it is true, a frightful appearance, but he no longer appeared to suffer any pain.
</textarea>
</div>
<button id="btnTest">CLICK ME</button>
</body>
</html>
Upvotes: 4
Reputation: 1863
Here's a reference from me:
<script>
function input(){
var text = "here the text that you want to input.";
document.forms.form1.area.value = text;
}
</script>
<form name='form1'>
Click<input onclick='input()' type='button' value='BUTTON' id='button'><br>
<textarea name='area'></textarea>
</form>
that an example that showing when a button clicked, a row of text added to textarea.
why do i added javascript in it? because html attributes only is not enough to make it.
may it help :D
Upvotes: 3