2

Unable to focus on textbox on button click:

<html ng-app="CommonApp">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-controller="HeadCtrl" ng-init='username=""'>
    <input id="UserName" type="text" class="login_boxdecor" ng-model="username" />
    <div class="login_btn_outer">
        <div class="login_btn_outer2" ng-click="cLgn();">
            <a class="btn">Login</a>
        </div>
    </div>
</body>
</html>
var myApp = angular.module('CommonApp', []);
    myApp.controller('HeadCtrl', function($scope){
        $scope.cLgn= function(){
            if($scope.username.trim().length==0)
            {
                //Here How to focus my textbox
            }
        };
    });

2 Answers 2

1
<html ng-app="CommonApp">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-controller="HeadCtrl" ng-init='username=""'>
    <input id="UserName" type="text" class="login_boxdecor" ng-model="username" />
    <div class="login_btn_outer">
        <div class="login_btn_outer2" ng-click="cLgn();">
            <a class="btn">Login</a>
        </div>
    </div>
</body>
</html>
var myApp = angular.module('CommonApp', []);
    myApp.controller('HeadCtrl', function($scope){
        $scope.cLgn= function(){
            if($scope.username.trim().length==0)
            {
                document.getElementById("UserName").focus();
            }
        };
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You can use angular.element to select your input box and can call focus(). Try something similar to below::

 <html ng-app="CommonApp">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-controller="HeadCtrl" ng-init='username=""'>
    <input id="UserName" type="text" class="login_boxdecor" ng-model="username"/>
    <div class="login_btn_outer">
        <div class="login_btn_outer2" ng-click="cLgn();">
            <a class="btn">Login</a>
        </div>
    </div>
</body>
</html>
var myApp = angular.module('CommonApp', []);
    myApp.controller('HeadCtrl', function($scope){
        $scope.focusText =false;
        $scope.cLgn= function(){
            if($scope.username.trim().length==0)
            {
                 angular.element("#UserName").focus();
            }
        };
    });

4 Comments

cLgn() is getting called?? Can you verify. See running code @ jsfiddle.net/deathhell/UTn5y/2
I want to focus my textbox on button click and the link you provided here is focus a textbox when mouse pointer focus a textbox.
Ohk, got it: I have just updated the answer, can u plz have a try ? put angular.element("#UserName").focus(); in your function call.
its not working but i have got the another solution by doing this document.getElementById("UserName").focus();

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.