»
EnglishFrenchVietnamese

Print - JavaScript Text Auto-Select onClick - JavaScriptBank.com

Full version: jsB@nk » Form » JavaScript Text Auto-Select onClick
URL: http://www.javascriptbank.com/javascript-text-auto-select-onclick.html

JavaScript Text Auto-Select onClick © JavaScriptBank.comIf this JavaScript code example installed on a web page, when users click on text-container HTML elements then it will select all of its inner text automatically.At present, this JavaScript code made to work on elements such as input, textarea, div, span, td, and pre. You still can edit it to work more HTML elements as you want. That's so helpful for website with a lot of text.

Full version: jsB@nk » Form » JavaScript Text Auto-Select onClick
URL: http://www.javascriptbank.com/javascript-text-auto-select-onclick.html



JavaScript
<script type="text/javascript">// Created by: Matt Murphy | http://www.matts411.com/// This script downloaded from www.JavaScriptBank.comfunction autoSelect(selectTarget) { if(selectTarget != null && ((selectTarget.childNodes.length == 1      && selectTarget.childNodes[0].nodeName == "#text") || (selectTarget.tagName == "INPUT"      && selectTarget.type == "text"))) {  if(selectTarget.tagName == 'TEXTAREA' || (selectTarget.tagName == "INPUT" && selectTarget.type == "text")) {   selectTarget.select();  } else if(window.getSelection) { // FF, Safari, Opera   var sel = window.getSelection();   var range = document.createRange();   range.selectNode(selectTarget.firstChild);   sel.removeAllRanges();   sel.addRange(range);  } else { // IE   document.selection.empty();   var range = document.body.createTextRange();   range.moveToElementText(selectTarget);   range.select();  } }}</script>


HTML
<h4 style="margin-bottom: 0;">A <code>div</code> Element:</h4><div onclick="autoSelect(this);">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultrices vestibulum elit. Mauris congue sapien sed dolor. Pellentesque sem augue, porttitor id, placerat ac, congue ac, eros. Etiam fermentum consectetuer pede. Donec tincidunt. Suspendisse non nisi. In hac habitasse platea dictumst. In hac habitasse platea dictumst. Integer porta egestas sapien.</div><h4 style="margin-bottom: 0;">An <code>input</code> Element:</h4><input type="text" size="50" onclick="autoSelect(this);" value="Lorem ipsum dolor sit amet, consectetuer adipiscing elit."><h4 style="margin-bottom: 0;">A <code>textarea</code> Element:</h4><textarea rows="5" cols="30" onclick="autoSelect(this);">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultrices vestibulum elit. Mauris congue sapien sed dolor. Pellentesque sem augue, porttitor id, placerat ac, congue ac, eros. Etiam fermentum consectetuer pede.</textarea><h4 style="margin-bottom: 0;">A <code>pre</code> Element:</h4><pre onclick="autoSelect(this);">function toggle_visibility(id) {  var e = document.getElementById(id);  if(e.style.display == 'none')    e.style.display = 'block';  else    e.style.display = 'none';}</pre>