0

I have an input :

<table>
...
<td style="width:59px"><input ng-model="myModel.propertyOne"  ng-blur="persistValue(myModel)"  enter-as-tab></td>
<td style="width:59px"><input ng-model="myModel.propertyTwo"  ng-blur="persistValue(myModel)"  enter-as-tab></td>

with its directive

angular.module('bioandbioApp').directive('enterAsTab', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                event.preventDefault();
                var elementToFocus = element.next('td').find('input')[1];
                if(angular.isDefined(elementToFocus))
                    elementToFocus.focus();
            }
        });
    };
});

This directive is called when user press Enter, and set the focus to the input of the next tag.

My problem is that the directive is called but elementToFocus.focus() is not called so the focus is not set.

I don't understand why. Does anyone know?

http://jsfiddle.net/tomy29/vpaqt29d/105/

Thanks

1
  • 1
    Care to create a fiddle or plunker? Commented Sep 7, 2015 at 8:00

3 Answers 3

1

You need to find the parent first and then go the next element.

Try the below code:

var app = angular.module("ap", []);

app.controller("con", function ($scope) {

    $scope.persons = [{
        name: 'Susan',
        age: 1
    }, {
        name: 'Peter',
        age: 1
    }, {
        name: 'Jack',
        age: 2
    }];
});

app.directive('enterAsTab', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 13) {
                event.preventDefault();
                console.log("parent");
                console.log(element.parent())
                var elementToFocus = element.parent().next('td').find('input')[0];
                console.log("next element");
                console.log(elementToFocus);
                if (angular.isDefined(elementToFocus)) elementToFocus.focus();
            }
        });
    };
});


JSFiddle

--EDIT--
This definitely can be optimized but a crud way of doing is as below:

<body ng-app="ap" ng-controller="con">
     <h4>Pressing <i>Enter/Return</i> in the <i>Age</i> field will iterate through the ages</h4>

    <table>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr ng-repeat='person in persons'>
            <td>
                <input type='text' name="personName" ng-model="person.name" enter-as-tab />
            </td>
            <td>
                <input type='number' name="personAge" ng-model="person.age" enter-as-tab/>
            </td>
        </tr>
    </table>
</body>

var app = angular.module("ap", []);

app.controller("con", function ($scope) {

    $scope.persons = [{
        name: 'Susan',
        age: 1
    }, {
        name: 'Peter',
        age: 1
    }, {
        name: 'Jack',
        age: 2
    }];
});

app.directive('enterAsTab', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 13) {
                event.preventDefault();
                console.log("parent");
                console.log(element.parent())
                var elementToFocus = element.parent().next('td').find('input')[0];
                console.log('next element');
                console.log(elementToFocus);
                if (angular.isDefined(elementToFocus)) {
                    elementToFocus.focus();
                } else {
                    element.parent().parent().next('tr').find('input')[0].focus();
                }
            }
        });
    };
});

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is not taking the focus to the next input element when in the age input box. I think the proper solution would be to take the focus to the next input element in the table.
0

you can use this code

$(this).closest('td').next('td').find('input')[0];

Currently, this is the input field of the current td. So, by that, you can find the input to the next of the current td.

Here is the working JSFIDDLE

1 Comment

Your solution is not taking the focus to the next input element when in the age input box. I think the proper solution would be to take the focus to the next input element in the table.
0

Although your fiddle is using static contents and a fixed set of fields, below is how I achieved the solution, however, depending on the dynamic content and your requirements, the solution may vary:

app.directive('enterAsTab', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                event.preventDefault();
                var elementToFocus;

                if(attrs["name"] == "personAge"){                 
                    elementToFocus = element.parent().parent().next().find('input')[0];
                }else{
                    elementToFocus = element.parent().next().find('input')[0];
                }

                if(angular.isDefined(elementToFocus))
                    elementToFocus.focus();
            }
        });
    };
});

2 Comments

it goes to the next input, but in the next <tr> , not in the same <tr>
Updated the answer as per your fiddle.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.