Chiya Chiya - 5 months ago 13
jQuery Question

How do I get the value of an input element using angular.element('#id')

If I trying to get the value of input element using angular.element but its returning undefined value.
Here is my code:


$scope.registerUser = function() {

console.log(angular.element('#username').value); // undefined
console.log(document.getElementById('username').value); // seshu


};

How do I get the value using angular.element

Answer

You should use val method similar to jQuery's $.fn.val:

console.log(angular.element('#username').val());

Alternatively you can use value property of the pure HTMLInputELement:

console.log(angular.element('#username')[0].value);

... because angular.element instance is an array-like collection of HTMLElements with every element accessible by its index.

Comments