vr3w3c9
vr3w3c9

Reputation: 1178

Get the Id of the element in Angular js

I have assigned the id value of an element in angularjs to a variable like below

var test = angular.element("#sqldiv");

I'm sending the test variable to a function and wanted to use the id of the element in the function. I'm unable to retrieve the id value. when I try to put an alert for test. it just give [object,Object] as the output.Please let me know how to retrieve the id value of the element. i.e I wanted the value as "#sqldiv".

Any help on this is much appreciated. Thanks in Advance.

Upvotes: 0

Views: 12016

Answers (2)

lenilsondc
lenilsondc

Reputation: 9800

Angular's jQlite works like jQuery with a little limitation of features to esure a good performance so that you can use jQuery's method $.fn.attr to do such thing.

For example:

var elm = angular.element(document.getElementById("#sqldiv"));

console.log(elm.attr('id'));

Note on angular.element: Its selector are limited so you can't query by id like that, you have to pass the element it self to jQlite like: angular.element(document.getElementById("#sqldiv")) or angular.element(document.querySelector("#sqldiv"))

Note on using DOM with angular: As it's been told on comments, avoid using DOM for data binding, angularjs has its own way to do that, most of activities doesn't require DOM manipulation, you should create a directive to do such thing, where you have the appropriated way to handle it.

Upvotes: 2

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

try this

test[0].id

This will give you id of element.

Upvotes: 1

Related Questions