0

Using angularjs

I am trying to reference an attribute from an element when a hotkey is pressed. In the boxes in the example, I would like to click alt-E and then get the value in an attribute on the element.

<body ng-controller="Ctrl" ng-keydown="down($event)">
<h1>Test HotKey!</h1>    
 <div><input data-box="65"></input></div>
 <div><input data-box="234"></input></div>
 <div><input data-box="9"></input></div>
 <div><input data-box="32"></input></div>
 <div><input data-box="13"></input></div>

The js looks like this

var myApp = angular.module('myApp', []);

myApp.controller('Ctrl', function($scope) {

   $scope.down = function($event) {

      console.log($event);
      console.log($scope);

      if($event.altKey === true ) {
          alert($event.target.attributes);
    }
  };

});

in this example when the alt-E is clicked I need the attribute value in data-box?

1 Answer 1

1

What about using the scrElement property on the event to access the dom element and its attributes. E.g.:

 myApp.controller('Ctrl', function ($scope) {
        $scope.down = function ($event) {
            var elem = angular.element($event.srcElement);
            console.log(elem.attr("data-box"));
        };
    });
Sign up to request clarification or add additional context in comments.

Comments

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.