dojo.getAttr

Gets an attribute on an HTML element.

Introduction

dojo.getAttr() is a companion function for dojo.attr. It handles normalized getting of attributes on DOM Nodes and return the value of the requested attribute or null if that attribute does not have a specified or default value.

Since Dojo 1.7, dojo.getAttr is exposed via the get method of the dojo/dom-attr module. An alias is kept in dojo/_base/html for backward-compatibility.

Usage

// Dojo 1.7+ (AMD)
require(["dojo/dom-attr"], function(domAttr){
  domAttr.get(node, attr);
});

// Dojo < 1.7
dojo.getAttr(node, attr);
node
id or reference to the element to get the attribute on.
attr
the name of the attribute to get.

Examples

Dojo 1.7 (AMD)

When using AMD format in a fully baseless application, get is accessed from the dojo/dom-attr module.

require(["dojo/dom-attr", "dojo/dom"], function(domAttr, dom){
    // get the current value of the "foo" attribute on a node
    domAttr.get(dom.byId("nodeId"), "foo");

    // or we can just pass the id:
    domAttr.get("nodeId", "foo");
});

Alternatively, you can load dojo base in AMD style and continue using dojo.getAttr in the define or require callback:

require(["dojo"], function(dojo){
    // get the current value of the "foo" attribute on a node
    dojo.getAttr(dojo.byId("nodeId"), "foo");

    // or we can just pass the id:
    dojo.getAttr("nodeId", "foo");
});

Dojo < 1.7

// get the current value of the "foo" attribute on a node
dojo.getAttr(dojo.byId("nodeId"), "foo");

// or we can just pass the id:
dojo.getAttr("nodeId", "foo");
Error in the documentation? Can’t find what you are looking for? Let us know!