Jquery - If cursor position at the start/end in a textfield
Asked Answered
V

2

9

i have a textarea and I want to check if the cursor is at the start or at the end (I dont need the current position).
Did anybody know a simple jQuery solution?

Thanks in advance!
Peter

Voroshilovgrad answered 11/10, 2010 at 8:14 Comment(3)
Do you mean <input type="text"> or a <textarea>?Osbourn
I mean <input type="text">. Is textbox the current word?Voroshilovgrad
Guess so. I'm never certain what people mean.Osbourn
O
8

Assuming you mean a <input type="text"> rather than a textarea, here's a non-jQuery solution (that you can still use with your jQuery code). It will almost certainly be less code than a jQuery plug-in.

UPDATE 12 November 2011

Having said that, I have developed a jQuery plug-in for just his kind of task, and it is indeed bigger than the code below.

var textInput = document.getElementById("your_id"), val = textInput.value;
var isAtStart = false, isAtEnd = false;
if (typeof textInput.selectionStart == "number") {
    // Non-IE browsers
    isAtStart = (textInput.selectionStart == 0);
    isAtEnd = (textInput.selectionEnd == val.length);
} else if (document.selection && document.selection.createRange) {
    // IE <= 8 branch
    textInput.focus();
    var selRange = document.selection.createRange();
    var inputRange = textInput.createTextRange();
    var inputSelRange = inputRange.duplicate();
    inputSelRange.moveToBookmark(selRange.getBookmark());
    isAtStart = inputSelRange.compareEndPoints("StartToStart", inputRange) == 0;
    isAtEnd = inputSelRange.compareEndPoints("EndToEnd", inputRange) == 0;
}

alert("At start: " + isAtStart + ", at end: " + isAtEnd);
Osbourn answered 11/10, 2010 at 9:3 Comment(4)
Is this answer still valid as of Jan 2016?Fennec
@Jessica: Absolutely, and it will almost certainly be valid for the foreseeable future since selectionStart and selectionEnd are standardized. The second branch is only for IE 8 and earlier so could be removed if you don't need to worry about those browsers.Osbourn
Thanks! Do you suggest I support IE 8 and earlier?Fennec
@Jessica: That really depends on your audience but in general I'd say not, particularly since Microsoft themselves have recently stopped supporting older versions of IE.Osbourn
R
2

Yep, use this awesome jQuery plugin.

Need more info? Come back here with more questions :)

Update
The original blog post associated with the plugin is down.

Thanks to the Awesome Power of the wayback machine, you can still view it.

http://web.archive.org/web/20080620163228/http://blog.0xab.cd/jquery-plugin---fieldselection

God I love that website.

Rubberize answered 11/10, 2010 at 8:17 Comment(5)
Very nice! Thank you for the fast answer!Voroshilovgrad
No worries @Peter, I hope the plugin helps :)Rubberize
Can you give me a little example how to use? The page "blog.0xab.cd/jquery-plugin---fieldselection" is unfortunately down :(Voroshilovgrad
@Voroshilovgrad - see my update. You can view the blog post using the Wayback machine.Rubberize
Wayback machine, nice idea ^^. Thanks!Voroshilovgrad

© 2022 - 2024 — McMap. All rights reserved.