How to test if event.target.hasClass() using angularJS and jqlite?
Asked Answered
V

3

6

After a click pass the event to ctrl. I want to write a conditional that will return true if the element.target has the class modal-click-shield

Question:

How can I use .hasClass() with event.target using angulars' jqlite?

Problem:

Currently I'm getting a type error saying that:

$scope.exitModal = function(event){
        // Return to current page when exiting the modal, via UI.
        // After state return, should set focus on the matching link.
        var target = event.target;
        console.log(target.hasClass('modal-click-shield'));
});

Error:

TypeError: undefined is not a function

Html:

  <div class="modal-click-shield" ng-click="exitModal($event)">
     <div ui-view="pdw"  class="product-container"></div>
  </div>
Vainglorious answered 21/1, 2015 at 0:4 Comment(1)
angular.element(event.target).hasClass('modal-click-shield')Joseph
I
18

Your element from event.target is a regular HTMLElement, not the JQlite version. You need to do this to convert it:

angular.element(event.target);
Ingle answered 21/1, 2015 at 0:14 Comment(0)
A
13

If you want to keep using your JS DOM element plain without use jQuery or angular:

event.target.classList.contains('modal-click-shield')
Angeloangelology answered 19/5, 2017 at 9:50 Comment(1)
Best compatibility for Angular 1/2Holocrine
C
4

because event.target is a DOM node, not a "jQuery" object. Wrap it

var target = $(event.target);

or

angular.element(event.target);
Caylacaylor answered 21/1, 2015 at 0:6 Comment(2)
I'm not useing jQuery. how can I convert it to a jqlite object?Vainglorious
ReferenceError: $ is not definedVainglorious

© 2022 - 2024 — McMap. All rights reserved.