-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
70 lines (45 loc) · 1.51 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "Element: setAttributeNode() method"
short-title: setAttributeNode()
slug: Web/API/Element/setAttributeNode
page-type: web-api-instance-method
browser-compat: api.Element.setAttributeNode
---
{{ APIRef("DOM") }}
The **`setAttributeNode()`** method of the {{domxref("Element")}} interface adds a new {{domxref("Attr")}} node to the specified element.
If you don't need to work with the attribute node (such as cloning from another element) before adding it, you can use the {{domxref("Element.setAttribute()", "setAttribute()")}} method instead.
## Syntax
```js-nolint
setAttributeNode(attribute)
```
### Parameters
- `attribute` is the `Attr` node to set on the element.
### Return value
The replaced attribute node, if any, returned by this function.
## Examples
This example copies the `align` attribute from one element to another.
### HTML
```html
<div id="one" align="left">one</div>
<div id="two">two</div>
```
### JavaScript
```js
let d1 = document.getElementById("one");
let d2 = document.getElementById("two");
let a = d1.getAttributeNode("align");
d2.setAttributeNode(a.cloneNode(true));
// Returns: 'left'
alert(d2.attributes[1].value);
```
## Notes
If the attribute named already exists on the element, that attribute is replaced with
the new one and the replaced one is returned.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("Document.createAttribute()")}}
- {{domxref("Element.getAttributeNode()")}}
- {{domxref("Element.removeAttributeNode()")}}