Forum Moderators: open
I can get the focus/cursor at the end of the textarea value by
document.getElementById("foo").value = "Happy kittens dancing"
document.getElementById("foo").focus()
Is there a way to get the focus/cursor after the word kittens?
Or even is there a way to find the focus/cursor position if the user clicks anywhere between the string?
have a look at this example, it may suit your requirements...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>set cursor position</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><script type="text/javascript">
window.onload=function() {
setCursor(document.getElementById('input1'),13,13)
}
function setCursor(el,st,end) {
if(el.setSelectionRange) {
el.focus();
el.setSelectionRange(st,end);
}
else {
if(el.createTextRange) {
range=el.createTextRange();
range.collapse(true);
range.moveEnd('character',end);
range.moveStart('character',st);
range.select();
}
}
}
</script></head>
<body><div>
<textarea id="input1" name="input1" rows="10" cols="30">Happy kittens dancing</textarea>
</div></body>
</html>
birdbrain
No problem, you're welcome. ;)