Jump to content

Keystrokes with Javascript


blackcell

Recommended Posts

ah, then that'd be a Range.

 

Here are some fns that came from quirksmode

function getcaret(elm) 
{
var caret = 0;
// IE Support
if (document.selection) 
{
	elm.focus ();
	var sel = document.selection.createRange ();
	sel.moveStart ('character', -elm.value.length);
	caret = Sel.text.length;
}

// Firefox support
else if (elm.selectionStart || elm.selectionStart == '0')
	caret = elm.selectionStart;

return (caret);
};

function setcaret(elm, pos)
{
if(elm.setSelectionRange)
{
	elm.focus();
	elm.setSelectionRange(pos,pos);
}
else if (elm.createTextRange) {
	var range = elm.createTextRange();
	range.collapse(true);
	range.moveEnd('character', pos);
	range.moveStart('character', pos);
	range.select();
}
};

 

So your textarea might look like this to set the caret at the start when clicked.

 

<textarea name="foo" onfocus="setcaret(this,0)">asdf asdf asdf asdf </textarea>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.