Javascript Form How to - Select all text in a Text Input field Box








Question

We would like to know how to select all text in a Text Input field Box.

Answer


 <!--  ww w. ja  v a  2  s .c o m-->
<html>
<head>
<script type="text/javascript">
function selectText() {
   var oTextbox1 = document.getElementById("txt1");
   oTextbox1.focus();
   oTextbox1.select();
}
</script>
</head>
<body>
<input type="text" size="12" id="txt1" value="www.java2s.com" /><br />
<input type="button" value="Select Text" onclick="selectText()" />
</body>
</html>

The code above is rendered as follows: