views:

35

answers:

2

Hi,

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

+2  A: 

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.

Marko
Very nice! Thank you for the fast answer!
Peter
No worries @Peter, I hope the plugin helps :)
Marko
Can you give me a little example how to use? The page "http://blog.0xab.cd/jquery-plugin---fieldselection" is unfortunately down :(
Peter
@Peter - see my update. You can view the blog post using the Wayback machine.
Marko
Wayback machine, nice idea ^^. Thanks!
Peter
+1  A: 

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.

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 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);
Tim Down