From 4d7a23c1c72d9ccf30143feee9ffc834213987ad Mon Sep 17 00:00:00 2001 From: bendtherules Date: Thu, 15 Oct 2020 22:43:26 +0530 Subject: [PATCH] create-modify-element - Shorten code samples --- docs/create-modify-element.mdx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/create-modify-element.mdx b/docs/create-modify-element.mdx index 78cb9e1..64a7aaf 100644 --- a/docs/create-modify-element.mdx +++ b/docs/create-modify-element.mdx @@ -16,10 +16,10 @@ import Accordion from "../components/Accordion" This method returns the **first element that matches the CSS selector**. If no match is found, it returns `null`. ```js -var ele = document.querySelector(selector); +document.querySelector(selector); -// Ex. - body > div.container > div.box + div.box -document.querySelector(".box"); // returns first div.box +// Ex. - body > div.container > div.box +document.querySelector(".box"); // returns div.box document.querySelector(".not-found"); // null ``` @@ -51,9 +51,9 @@ Here, the NodeList is static - meaning any changes in the DOM later does not aff ::: ```js -var paragraphs = document.querySelectorAll("p"); +var paras = document.querySelectorAll("p"); -for (var p of paragraphs) { +for (var p of paras) { p.className = "note"; } ``` @@ -69,7 +69,7 @@ The `document.createElement` method creates a new HTML element, taking the tag n To set attributes and add the element to DOM, look at `.setAttribute` and `.appendChild` below. ```js -var newEle = document.createElement("div") // creates
+document.createElement("div") // creates
``` @@ -78,10 +78,10 @@ var newEle = document.createElement("div") // creates
`createTextNode` method takes a string as input and returns a new TextNode. ```js - var newEle = document.createElement("div") + var ele = document.createElement("div") var newText = document.createTextNode("Hello world") - newEle.appendChild(newText) //
Hello world
+ ele.appendChild(newText) //
Hello world
``` @@ -96,9 +96,10 @@ Every element has a `.setAttribute(name, value)` method, which can add/modify a `value` - value of the attribute. Ex- `"text"`. ```js -var newInput = document.createElement("input") +var ele = document.createElement("input") -newInput.setAttribute("type", "text") // +ele.setAttribute("type", "text") +// creates - ``` [Read on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute) \ No newline at end of file