How to replace the currently selected text inside an html textarea?
By : Edson Medina
Date : March 29 2020, 07:55 AM
I hope this helps you . How do I edit the selected text of a textarea form element? , This works: code :
function replaceIt(txtarea, newtxt) {
$(txtarea).val(
$(txtarea).val().substring(0, txtarea.selectionStart)+
newtxt+
$(txtarea).val().substring(txtarea.selectionEnd)
);
}
$("button").on('click', function() {
replaceIt($('textarea')[0], 'fun')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>Hello world.</textarea>
<button>Replace with fun</button>
|
How to surround selected text in PyCharm like with Sublime Text
By : RAJESHKUMAR
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I think you want something like Settings | Editor | General | Smart Keys -> Surround selection on typing quote or brace
|
JavaScript: Selected text is not getting replaced when the text is inside a textarea
By : Ethernaly
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The problem is that the text inside a textarea cannot be accessed using window.getSelection. To access the text, there are different selection API namely selectionStart (The index of the beginning of selected text) and selectionEnd (The index of the end of selected text). code :
function ShowSelectionInsideTextarea(replacementText, eleID)
{
var textComponent = document.getElementById(eleID);
if (textComponent.selectionStart != undefined)
{
var mainStr = document.getElementById(eleID).value;
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos);
mainStr= mainStr.replace(selectedText, replacementText);
document.getElementById(eleID).innerHTML=mainStr;
}
}
|
Find out if a selected text is inside a input field or textarea
By : Denis Setianto
Date : March 29 2020, 07:55 AM
it helps some times Here is the sample code, in which the alert message is shown only if the mouseup event came from element that is not textarea or input of type text. EDITED CODE: code :
$(function() {
var isInputOrTextarea = false;
$(document).on("mousedown", function(e) {
// On mousedown flag whether it was clicked in input or textarea
if($(e.target).is("textarea,input[type=text]")) {
isInputOrTextarea = true;
}
});
$(document).on("mouseup", function(e) {
if($(e.target).is("textarea,input[type=text]") || isInputOrTextarea) {
isInputOrTextarea = false;
return;
}
isInputOrTextarea = false;
alert("Not in text area or input");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" value="Lorem ipsum"/>
<textarea>
Lorem ipsum
</textarea>
<p>
Lorem ipsum
</p>
|
how to get selected text inside a textarea element by javascript?
By : user3782414
Date : March 29 2020, 07:55 AM
Hope this helps I'm not familiar with such attributes,
|