1

I have my app with tons of buttons/inputs/etc. with different events. I want to clearly identify each one of them which some event triggers on.

For example, when I have a piece of my app:

<div class="someClass">
  <div>
    <someOtherElement>
      <div></div>
      <div><button ng-click="someClickEvent($event)"></button></div>
    </someOtherElement>
  </div>
</div>

I want to identify somehow, which button I have just clicked:

function someClickEvent(e) {
   // some identification code here
}

[edit]

Maybe I wrote this wrong... I want some identification like XPath or something that will point which button were triggered (for error logging purposes).

So when I click my button and some error occurs, I want to identify the button and log some information about it (e.g. div[0].someClass>div[0]>someOtherElement[0]>div[1]>button[0]).

1
  • Do you want to identify the button with the id attribute? Commented Oct 10, 2017 at 6:25

3 Answers 3

1

You can get identify the button and log it by this:

$scope.clickFunc = function(event){
   $scope.clickedElement = event.target.outerHTML;
};

DEMO: http://jsfiddle.net/rjdzuxaL/1/

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

Comments

0

Use ng-click instead on onclick

<button  ng-click="myFunction($event)">test</button>

Working demo

Comments

0

Change HTML to:

<button  ng-click="myFunction($event)">test</button> //onclick works on javascript. For Angularjs, use ng-click.

JS:

$scope.someClickEvent = function(e) {
  // some identification code here
  var element = e.target; // this will give you the reference to the element.
}

You should avoid handling DOM in the controller. Use directives for them.

1 Comment

Note: e.target will be a reference to the node clicked. e.currentTarget will be a reference to the node you attached your event handler to. Sometimes e.target !== e.currentTarget, for example when clicking on a child node of e.currentTarget. source

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.