ProcessingInstruction: setAttribute() method
The setAttribute() method of the ProcessingInstruction interface sets the value of an attribute on the processing instruction.
If the attribute already exists, the value is updated; otherwise, a new attribute is added with the specified name and value.
Syntax
setAttribute(qualifiedName, value)
Parameters
qualifiedName-
A string containing the qualified name of the attribute whose value is to be set.
The format of the qualified name is
prefix:localNameorlocalName, where the parts are defined as:prefixOptional-
A "short alias" for the namespace, as returned by the
Attr.prefixproperty. localName-
The local name of the attribute, as returned by the
Attr.localNameproperty.
value-
A string containing the value to assign to the attribute.
Specified non-string values are automatically converted into strings.
For boolean attributes, you should, by convention, set
valueto the empty string ("") or the attribute's name, with no leading or trailing whitespace.
Return value
None (undefined).
Exceptions
InvalidCharacterErrorDOMException-
Thrown if either the
prefixorlocalNameis not valid:- The
prefixmust have at least one character, and cannot contain ASCII whitespace,NULL,/, or>(U+0000, U+002F, or U+003E, respectively). - The
localNamemust have at least one character, and may not contain ASCII whitespace,NULL,/,=or>(U+0000, U+002F, U+003D, or U+003E, respectively).
- The
Description
setAttribute() sets the value of an attribute on the specified processing instruction.
If the attribute already exists, the value is updated; otherwise, a new attribute is added with the specified name and value.
To set the value of a Boolean attribute, such as disabled, you can specify any value.
It doesn't matter what value you use; if the attribute is present, its value is considered to be true.
By convention, we enable boolean attributes by setting their value to either the name of the attribute or the empty string ("").
The absence of a boolean attribute means its value is false; you must call ProcessingInstruction.removeAttribute() to "undo" the effect of enabling a boolean attribute.
To get the current value of an attribute, use getAttribute(); to remove an attribute, call removeAttribute().
Examples
>Basic usage
const pi = document.createProcessingInstruction("start", 'name="placeholder"');
pi.setAttribute("name", "new text");
console.log(pi);
// logs:
// <?start name="new text"?>
Specifications
| Specification |
|---|
| DOM> # dom-processinginstruction-setattribute> |