AngularJS - get element attributes values
Asked Answered
D

8

47

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/

Directed answered 10/7, 2014 at 9:51 Comment(1)
Should be item.getAttribute('data-id')Aitch
B
56

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
}
Birdseed answered 10/7, 2014 at 9:55 Comment(4)
Is this attributes part of the DOM or is this something AngularJS brings to the table?Directed
part of the DOM - not angular specificBirdseed
But how is it used in ng-click ?Sludgy
if data-id is {{ data }} it return "{{ data }}" instead the value of data :(Laicize
S
18

You just can use doStuff($event) in your markup and get the data-attribute values via currentTarget.getAttribute("data-id")) in angular. HTML:

<div ng-controller="TestCtrl">
    <button data-id="345" ng-click="doStuff($event)">Button</button>
</div>

JS:

var app = angular.module("app", []);
app.controller("TestCtrl", function ($scope) {
    $scope.doStuff = function (item) {
        console.log(item.currentTarget);
        console.log(item.currentTarget.getAttribute("data-id"));
    };
});

Forked your initial jsfiddle: http://jsfiddle.net/9mmd1zht/116/

Swithbert answered 18/1, 2016 at 9:38 Comment(2)
As far as I know it works on all current Browsers. All browsers angularJS is running on.Swithbert
tested on angluar 6Boom
Y
16

the .data() method is from jQuery. If you want to use this method you need to include the jQuery library and access the method like this:

function doStuff(item) {
  var id = $(item).data('id');
}

I also updated your jsFiffle

UPDATE

with pure angularjs and the jqlite you can achieve the goal like this:

function doStuff(item) {
  var id = angular.element(item).data('id');
}

You must not access the element with [] because then you get the pure DOM element without all the jQuery or jqlite extra methods.

Yamashita answered 10/7, 2014 at 10:0 Comment(6)
As per AngularJS documentation,jQLite with the data() is included in the AngulaJS source: docs.angularjs.org/api/ng/function/angular.element , hence I assumed the API for the data() method is the same as jQueryDirected
you're right and wrong: the .data() method is the same, but if you access the element with [] then you you get a pure DOM element and loose all extra functionality added by jqlite or jQueryYamashita
Can you get a specific attr with .data('key')?Directed
No, with .data('key') you get only attributes starting with data- (in you example the value of data-key="someValue", if you want to access other attributes, you must use .attr('attributeName')Yamashita
...and this is why custom attribute names should start with 'data-' (best practice).Rustle
This post is old. Now you may have to use .dataset.attrname . Use your dev console to see the object returned by .dataset .Rustle
B
0

You can do this using dataset property of the element, using with or without jquery it work... i'm not aware of old browser Note: that when you use dash ('-') sign, you need to use capital case. Eg. a-b => aB

function onContentLoad() {
  var item = document.getElementById("id1");
  var x = item.dataset.x;
  var data = item.dataset.myData;

  var resX = document.getElementById("resX");
  var resData = document.getElementById("resData");

  resX.innerText = x;
  resData.innerText = data;

  console.log(x);
  console.log(data);
}
<body onload="onContentLoad()">
  <div id="id1" data-x="a" data-my-data="b"></div>

  Read 'x':
  <label id="resX"></label>
  <br/>Read 'my-data':
  <label id="resData"></label>
</body>
Babysitter answered 28/10, 2015 at 13:32 Comment(0)
G
0

function onContentLoad() {
  var item = document.getElementById("id1");
  var x = item.dataset.x;
  var data = item.dataset.myData;

  var resX = document.getElementById("resX");
  var resData = document.getElementById("resData");

  resX.innerText = x;
  resData.innerText = data;

  console.log(x);
  console.log(data);
}
<body onload="onContentLoad()">
  <div id="id1" data-x="a" data-my-data="b"></div>

  Read 'x':
  <label id="resX"></label>
  <br/>Read 'my-data':
  <label id="resData"></label>
</body>
Guillory answered 29/11, 2016 at 6:2 Comment(0)
L
0
<button class="myButton" data-id="345" ng-click="doStuff($element.target)">Button</button>

I added class to button to get by querySelector, then get data attribute

var myButton = angular.element( document.querySelector( '.myButton' ) );
console.log( myButton.data( 'id' ) );
Lozenge answered 10/5, 2017 at 12:23 Comment(0)
T
0

If you are using Angular2+ following code will help

You can use following syntax to get attribute value from html element

//to retrieve html element

const element = fixture.debugElement.nativeElement.querySelector('name of element'); // example a, h1, p

//get attribute value from that element

  const attributeValue = element.attributeName // like textContent/href
Taxiway answered 11/7, 2018 at 10:46 Comment(0)
F
-1

Use Jquery functions

<Button id="myPselector" data-id="1234">HI</Button> 
console.log($("#myPselector").attr('data-id'));
Fleck answered 10/7, 2014 at 10:1 Comment(2)
with jQuery it would be better $("#myPselector").data('id')Yamashita
I am not able to get attr value using $('#datasheet_link').data('href') but able to get value using '$('#datasheet_link').attr('href')' . Any ideas ?Simonson

© 2022 - 2024 — McMap. All rights reserved.