Este es un ejemplo adaptado de la
faq 251:
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ejemplo</title>
<script>
function sel(inicio,fin){
input=document.getElementById('area');
if(typeof document.selection != 'undefined' && document.selection){
tex=input.value;
input.value='';
input.focus();
var str = document.selection.createRange();
input.value=tex;
str.move('character', inicio);
str.moveEnd("character", fin-inicio);
str.select();
}
else if(typeof input.selectionStart != 'undefined'){
input.setSelectionRange(inicio,fin);
input.focus();
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input name="area" type="text" id="area" value="hola" />
<input type="button" name="Submit" value="seleccionar" onclick="sel(2,2)" />
</form>
</body>
</html>