Skip to content

Instantly share code, notes, and snippets.

@cuduy197
Created April 28, 2017 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuduy197/7b7ed5927b54df420c2b8fb89c1430f6 to your computer and use it in GitHub Desktop.
Save cuduy197/7b7ed5927b54df420c2b8fb89c1430f6 to your computer and use it in GitHub Desktop.
Get selected text in textarea !!!!!!!!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<title>Get selected text </title>
</head>
<body>
<form class="w3-container">
<h2>Selected text : </h2>
<mark id="result"></mark>
<br>
<hr>
<textarea onclick="showResult()" class="w3-input w3-animate-input" rows="4" cols="50" style="width:100%" >
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
</form>
<script>
function getText(input) {
return input.value.substring(input.selectionStart, input.selectionEnd);
}
function showResult() {
var selectText = getText(document.getElementsByTagName("textarea")[0]);
var selectText = getText(document.getElementsByTagName("textarea")[0]);
document.getElementById('result').innerHTML = selectText;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment