9

Does someone knows how to get the following working:

If an user clicks inside "name" - Set CSS Class to XYZ on DIV ?

<div ng-class="???">Enter your Name here</div>

<input type="text" ng-model="user.name" required id="name"/>

Version: AngularJS v1.0.8

1
  • what version of angular are you using? Commented Oct 9, 2013 at 16:16

3 Answers 3

27

If you're using Angular 1.2.x, see ng-focus and ng-blur:

<div ng-class="{xyz: focused}">Enter your name here</div>
<input type="text" ng-model="user.name" ng-init="focused = false" ng-focus="focused = true" ng-blur="focused = false" id="name" required>

If you're using a 1.0.x version, nothing is stopping you from defining your own focus and blur directives based on Angular 1.2.x's:

/*
 * A directive that allows creation of custom onclick handlers that are defined as angular
 * expressions and are compiled and executed within the current scope.
 *
 * Events that are handled via these handler are always configured not to propagate further.
 */
var ngEventDirectives = {};
forEach(
  'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
  function(name) {
    var directiveName = directiveNormalize('ng-' + name);
    ngEventDirectives[directiveName] = ['$parse', function($parse) {
      return function(scope, element, attr) {
        var fn = $parse(attr[directiveName]);
        element.on(lowercase(name), function(event) {
          scope.$apply(function() {
            fn(scope, {$event:event});
          });
        });
      };
    }];
  }
);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, guys. I've updated my answer to include potential solutions for both stable and unstable versions.
@ArneRie You can adapt the code I pasted (which is copied from angular's source) to suit your needs. If you want to use it as-is, then you'll also need to include the directiveNormalize() function (and any functions it uses).
ng-init="focused = false" may not be necessary. the "focused" variable should already equal false when html is loaded
7

Just use this directive:

app.directive('ngFocusClass', function () {
  return ({
        restrict: 'A',
        link: function(scope, element) {
            element.focus(function () {
                element.addClass('focus');
            });
            element.blur(function () {
                element.removeClass('focus');
            });
        }
    });
});

1 Comment

Same as @Jigar, had to use on event for this to work.
2

Working example for pre-1.2.xxx versions: http://jsfiddle.net/atXAC/

In this example, the ng-customblur directive will fire a function() in your controller.

HTML:

<div ng-controller="MyCtrl">
  <div ng-class="{'active':hasFocus==true,'inactive':hasFocus==false}">Enter your Name here</div>
  <input type="text" ng-model="user.name" ng-click="hasFocus=true" ng-customblur="onBlur()" required id="name"/>
</div>

JS:

myApp.directive('ngCustomblur', ['$parse', function($parse) {
  return function(scope, element, attr) {
    var fn = $parse(attr['ngCustomblur']);      
    element.bind('blur', function(event) {        
      scope.$apply(function() {
        fn(scope, {$event:event});
      });
    });
  }
}]);

function MyCtrl($scope) {
    $scope.onBlur = function(){        
        $scope.hasFocus = false;
    }
} 

1 Comment

Nice but there a lot of divs and inputs, think there must be an other way (-:

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.