Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check with angular.js what is the type of an element

Tags:

angularjs

I received an html node and store it as angularjs element:

var el=angular.element(e.target);

How can i check if that element is of type "textarea" or "input" without using jQuery?

like image 921
amit Avatar asked Mar 29 '15 10:03

amit


People also ask

What is element in AngularJS?

element is an alias for the jQuery function. If jQuery is not available, angular. element delegates to AngularJS's built-in subset of jQuery, called "jQuery lite" or jqLite. jqLite is a tiny, API-compatible subset of jQuery that allows AngularJS to manipulate the DOM in a cross-browser compatible way.

What is the use of NG Dblclick?

The ng-dblclick Directive in AngluarJS is used to apply custom behavior when an element is clicked double. It can be used to show or hide some element or it can popup an alert or change the color of text when it is clicked twice.

Can we use document getElementById in AngularJS?

You can use document. getElementById() in Angularjs, but manipulating HTML in a controller is not a good practice. I recommend you to create a directive and do this kind of stuff there.

What is this in AngularJS?

"How does this and $scope work in AngularJS controllers?" Short answer: this. When the controller constructor function is called, this is the controller. When a function defined on a $scope object is called, this is the "scope in effect when the function was called".


2 Answers

You can easily find it by using :-

 el[0].tagName=="INPUT" or  el[0].tagName=="TEXTAREA"

Small example:-

 <div ng-controller="MyCtrl">
    <input type="text" name="name" id="name"/><br>
    <textarea id="check"></textarea>
</div>

Controller:-

  var e1=angular.element(document.querySelector("#name"));
    var e2=angular.element(document.querySelector("#check"))
    console.log(e1[0].tagName);
    console.log(e2[0].tagName);

Fiddle

like image 108
squiroid Avatar answered Sep 21 '22 20:09

squiroid


A rather short answer is:

el[0].tagName === 'TEXTAREA'

like image 37
Pavel Horal Avatar answered Sep 20 '22 20:09

Pavel Horal