/    Sign up×
Community /Pin to ProfileBookmark

Is the caret at the start of the text input?

Hello,
I’m am looking for a way to figure out if the caret (text cursor) is at the start of a form’s text input (not textarea).
I want to check wether the caret is at the start and if so to perform some operation in the onKeyUp of the text input.
Does anyone have an idea?
Thanks,
Aviv

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@theBody44May 20.2004 — Do you want to check if the cursor is in the first (left) position, or if the input value has a "<" or ">" in it?? Do you mean you want to run the operation if the text box is empty??
Copy linkTweet thisAlerts:
@PittimannMay 21.2004 — Hi!

Play with this, if you like:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var defaultText1="There is a selection in the text field."
var defaultText2="Cursor <b>is not</b> at the beginning of the text field."
var pos;
var begin;
var isSel;
function checkCursor(where) {
isSel=0;
begin=0;
if (document.selection) {//IE stuff
where.focus();
sel = document.selection.createRange();
if(sel.text==""){
sel.text='µµµ';//insert nonsense at cursor position for IE
dummy = where.createTextRange();
dummy.findText('µµµ');
dummy.select();
pos=where.value.indexOf('µµµ');//get position of nonsense's begin
begin=pos;
document.selection.clear();//delete nonsense
where.focus();
}
if(sel.text!=""){
isSel=1;
}
}//end IE stuff
else if (where.selectionStart || where.selectionStart == '0') {//Mozilla stuff
begin = where.selectionStart;
var end = where.selectionEnd;
if(end!=begin){
isSel=1;
}
}//end Mozilla stuff
if(isSel==0&&begin==0){
document.getElementById('posi').innerHTML='Cursor <b>is</b> at the beginning of the text field';//action to take if cursor is at start pos
}
else if(isSel==1){
document.getElementById('posi').innerHTML=defaultText1;//action to take if text is selected
}
else{
document.getElementById('posi').innerHTML=defaultText2;//action to take if cursor is not at start pos and no text is selected
}
}
//-->
</script>
</head>
<body onload="document.getElementById('posi').innerHTML=defaultText2;">
<form name=frm>
<input name="txt" value="abcdefgdefghijklmno" onclick="checkCursor(this);" onkeyup="checkCursor(this);"><br>
</form>
<span id="posi"></span>
</body>
</html>[/code]
Cheers - Pit
×

Success!

Help @avivgur spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...