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

22,327

Solution 1

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);

Solution 2

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

event.target.classList.contains('modal-click-shield')

Solution 3

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

var target = $(event.target);

or

angular.element(event.target);
Share:
22,327
Armeen Moon
Author by

Armeen Moon

I am a generalist; slowly becoming a specialist in Web Development. I mix art, design, and technology, to create effective experiences that deliver value at scale. My professional goals are simple: surround myself with smart, energetic, creative people while working on solving problems that matter. Specialties: Functional and Object Oriented JavaScript ,Angular+, AngularJS, AWS, CSS/SCSS, Vector/DOM motion graphics, semantic HTML, NodeJS, Golang, and passionate about learning i18n/l10n, a11y, and modern web workflow.

Updated on July 17, 2022

Comments

  • Armeen Moon
    Armeen Moon almost 2 years

    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>
    
  • Armeen Moon
    Armeen Moon over 9 years
    I'm not useing jQuery. how can I convert it to a jqlite object?
  • Armeen Moon
    Armeen Moon over 9 years
    ReferenceError: $ is not defined
  • Joel Davey
    Joel Davey about 6 years
    Best compatibility for Angular 1/2