Skip to content

Latest commit

 

History

History
353 lines (252 loc) · 10.3 KB

dom-attr.rst

File metadata and controls

353 lines (252 loc) · 10.3 KB

dojo/dom-attr

Project owner

Eugene Lazutkin

since

V1.7

This module defines the core Dojo DOM attributes API. This module will eventually be retired and wholly superseded by the dojo/dom-prop <dojo/dom-prop> module in the future. The standard return variable for this module is domAttr.

The deprecated legacy features are set in dojo/_base/html <dojo/_base/html>.

Features

has()

A function that checks if an attribute is present on a DOM node, and returns the truthy value if it is there, and falsy value otherwise.

Usage

Arguments

Argument Description
node id or reference of the DOM node to get/set style for
attr the attribute property name
result truthy, if the attribute is present, falsy otherwise

Examples

Checking to see if a particular node as an attribute.

get()

A function that 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.

Usage

Arguments

Argument Description
node id or reference to the element to get the attribute on
attr the name of the attribute to get

Examples

Getting some values from a node.

set()

A function that handles normalized setting of attributes on DOM Nodes. When passing functions as values, note that they will not be directly assigned to slots on the node, but rather the default behavior will be removed and the new behavior will be added using dojo.connect(), meaning that event handler properties will be normalized and that some caveats with regards to non-standard behaviors for onsubmit apply. Namely that you should cancel form submission using event.stop() <dojo/_base/event#dojo-stopevent> on the passed event object instead of returning a boolean value from the handler itself. It returns the DOM node.

Usage

Arguments

Argument Description
node id or reference to the element to set the attribute on
attr the name of the attribute to set, or a hash of key-value pairs to set
value the value to set for the attribute, if the name is a string

Examples

Here is an example of changing a value of an attribute:

Here is an example of using an object to set multiple attribute values:

remove()

Is a function that removes an attribute from a DOM node. It is modeled after DOM's removeAttribute, but unlike the latter it normalizes standard attribute names to smooth over differences between browsers, or to provide convenient aliases, (e.g., className is aliased to class).

Usage

Arguments

Argument Description
node id or reference to the element to remove the attribute on
attr the attribute name

Examples

Here is an example of removing the disabled attribute from a DOM node:

getNodeProp()

Is a companion function for domAttr.get <dojo/domAttr#domattr-get>. Unlike the latter it favors properties falling back on attributes, if a property was not present.

It is useful when you don't care if somebody set an attribute on a node in HTML, or not, but you want to read a default/current value, which is used by a browser. For example, if user didn't specify type attribute on input element, it is default value is "text". You don't need to know all defaults, or how browser interprets missing attributes exactly, just use domAttr.getNodeProp.

There is no corresponding setNodeProp. If you want to set a property value, use straight assignment.

Usage

Arguments

Argument Description
node id or reference to the element to get the property on
attr the attribute property name

Examples

The following example reads effective values from the input node.

See also

  • dojo/dom <dojo/dom> - Core DOM API
  • dojo/dom-class <dojo/dom-class> - Dojo DOM Class API
  • dojo/dom-prop <dojo/dom-prop> - DOM Property API
  • dojo/dom-style <dojo/dom-style> - DOM Style API
  • dojo/_base/html <dojo/_base/html> - Legacy API aliases