AngularJS - get element attributes values

Iladarsda picture Iladarsda · Jul 10, 2014 · Viewed 166.5k times · Source

How do you get an element attribute value?

e.g. HTML element:

<button data-id="345" ng-click="doStuff($element.target)">Button</button>

JS:

function doStuff(item){
    angular.element(item)[0].data('id'); // undefined is not a function
}

Any suggestions much appreciated, JSFIDDLE demo here:http://jsfiddle.net/h3TFy/

Answer

rob picture rob · Jul 10, 2014

Since you are sending the target element to your function, you could do this to get the id:

function doStuff(item){
    var id = item.attributes['data-id'].value; // 345
}