AP wrote:
I am trying to find the integer value for the cursor position in a text box,
in particular I want to find out if I am at the end of some text that I
typed or at the beginning.
The following will give you the position of the caret, the rest is a
matter of substring-ing the control value and determine whether the
position is the one required.
Few browsers support ranges, though; you can expect it to work on IE and
Mozilla, but probably no other agents yet. Limitations in supporting
browsers are to be sought in the function triggering (can you see
situations where the function won't be triggered while it should be?).
<form>
<input type="text" onkeyup="s(this)" onclick="s(this)">
<input type="text" name="position">
</form>
<script type="text/javascript">
function s(el){
var sel, rng, r2, i=-1;
if(typeof el.selectionStart=="number") {
i=el.selectionStart;
} else if(document.selection && el.createTextRange) {
sel=document.selection;
if(sel){
r2=sel.createRange();
rng=el.createTextRange();
rng.setEndPoint("EndToStart", r2);
i=rng.text.length;
}
} else {
el.onkeyup=null;
el.onclick=null;
}
el.form.elements["position"].value=i;
}
</script>