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.
getAttributeNames()None.
An Array of strings.
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).
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