ProcessingInstruction: getAttributeNames() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The getAttributeNames() method of the ProcessingInstruction interface returns the attribute names of the processing instruction as an Array of strings. If the processing instruction has no attributes it returns an empty array.

Syntax

js
getAttributeNames()

Parameters

None.

Return value

An Array of strings.

Description

Using getAttributeNames() along with getAttribute() is a memory-efficient and performant alternative to accessing ProcessingInstruction.data.

The names returned by getAttributeNames() are qualified attribute names, meaning that attributes with a namespace prefix have their names returned with that namespace prefix (not the actual namespace), followed by a colon, followed by the attribute name (for example, xlink:href). Any attributes without a namespace prefix have their names returned as-is (for example, href).

Examples

Basic usage

js
const pi = document.createProcessingInstruction(
  "start",
  'name="placeholder" more="info"',
);

console.log(pi.getAttributeNames());
// logs:
// ['name', 'more']

// Iterate over processing instruction's attributes
for (const name of pi.getAttributeNames()) {
  const value = pi.getAttribute(name);
  console.log(name, value);
}
// logs:
// name placeholder
// more info

Specifications

Specification
DOM
# dom-processinginstruction-getattributenames

Browser compatibility

See also