BUG FIXED (iPhone-style passwords)
That bug I knew I had with the password fields is now fixed! It was refreshing to veer away from the comfort of jQuery get my hands dirty in raw js.
I thought this was an interesting bug, which was you couldn’t delete a character from somewhere in the middle of your entry and have it be correctly duplicated in the hidden field that actually gets sent with the form. I needed to detect the position of the text cursor, which jQuery doesn’t do at all. The solution turned out to be simple; this is what I ended up with:
// Gets the current cursor position in a textfield
// to determine where to delete a character
getCursorPosition : function(field) {
// IE
if (document.selection) {
field.focus ();
var sel = document.selection.createRange();
sel.moveStart ('character', -field.value.length);
return sel.text.length;
}
// OTHERS
else if (field.selectionStart || field.selectionStart == '0')
return field.selectionStart;
// Something went wrong
else
return -1;
}
All that’s left now is turn it into a plugin and give people options.
See the result at: http://www.timmywillison.com/samples/iphone_passwords/iphone_passwords.html.