create-modify-element - Add setAttribute()

This commit is contained in:
2020-10-15 20:34:45 +05:30
parent d3bce29c20
commit b2838159cc
+17 -2
View File
@@ -62,7 +62,7 @@ for (var p of paragraphs) {
## Create / insert element
### 1. `createElement`
### 1. `createElement()`
The `document.createElement` method creates a new HTML element, taking the tag name (`"div"`) as first argument.
@@ -86,4 +86,19 @@ var newEle = document.createElement("div") // creates <div/>
</Admonition>
</Accordion>
[Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement)
[Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement)
### 2. `setAttribute()`
Every element has a `.setAttribute(name, value)` method, which can add/modify a attribute on the current element.
`name` - the attribute name which should be added. Ex- `"type"`.
`value` - value of the attribute. Ex- `"text"`.
```js
var newInput = document.createElement("input")
newInput.setAttribute("type", "text") // <input type="text"/>
```
[Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute)