From b2838159ccfa339c26ef79641b625c9d05e8cf29 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Thu, 15 Oct 2020 20:34:45 +0530 Subject: [PATCH] create-modify-element - Add setAttribute() --- docs/create-modify-element.mdx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/create-modify-element.mdx b/docs/create-modify-element.mdx index 73a60af..78cb9e1 100644 --- a/docs/create-modify-element.mdx +++ b/docs/create-modify-element.mdx @@ -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
-[Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement) \ No newline at end of file +[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") // +``` + +[Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute) \ No newline at end of file