-1

How do I check to see if the cursor position is in the front of the text or the last of the text.

<script  type="text/javascript">
    $(document).ready(function () {
        $('input').keyup(function (e) {
            if (e.which == 39 && <CURSOR IS FIRST OR LAST OF TEXT>)  {
                alert($('input').val);
                $(this).closest('td').next().find('input').focus();
                $(this).closest('td').next().find('input').select();
            }
         }
      }
<script>


<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<script  type="text/javascript">
$(document).ready(function () {
    $('input').keyup(function (e) {
        if (e.which == 39 && <selected text lenght> > 0)  {
            $(this).closest('td').next().find('input').focus();
            $(this).closest('td').next().find('input').select();
        }
        else if (e.which == 37) {
            $(this).closest('td').prev().find('input').focus();
            $(this).closest('td').prev().find('input').select();
        }
        else if (e.which == 40) {
            $(this).closest('tr').next().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus();
            $(this).closest('tr').next().find('td:eq(' + $(this).closest('td').index() + ')').find('input').select();
        }
        else if (e.which == 38) {
            $(this).closest('tr').prev().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus();
            $(this).closest('tr').prev().find('td:eq(' + $(this).closest('td').index() + ')').find('input').select();
        }
    });
});

1 Answer 1

2

You can use jQuery Caret plugin. Check this thread here and the plugin's homepage. You can then change your code to:

var pos= $(this).caret().start == $(this).caret().end ? $(this).caret().start : -1;
if (e.which === 39 && (pos === 0 || pos === $(this).val().length)) {
...

Hope it helped, Shakib

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.