Replace & Remove Elements 본문

2021 프론트 엔드 로드맵 따라가기/JS

Replace & Remove Elements

알 수 없는 사용자 2021. 5. 31. 12:48

Replace

 

// Create Element
const newHeading = document.createElement("h2");

// Add an id
newHeading.id = "task-title";

// New text node
newHeading.appendChild(document.createTextNode("Task List"));

// Get the old Heading
const oldHeading = document.querySelector("#task-title");

// Parent
const cardAction = document.querySelector(".card-action");

// Replace
cardAction.replaceChild(newHeading, oldHeading);

console.log(newHeading);

 

Remove

 

// Remove element
const lis = document.querySelectorAll("li");
const list = document.querySelector("ul");

// Remove list item
lis[0].remove();

// Remove child element
list.removeChild(lis[3]);

// Classes & Attr
const firstLi = document.querySelector("li:first-child");
const link = firstLi.children[0];

let val;

// Classes
val = link.className;
val = link.classList;
val = link.classList[0];
link.classList.add("test");
link.classList.remove("test");
val = link;

// Attributes
val = link.getAttribute("href");
val = link.setAttribute("href", "http://google.com");
link.setAttribute("title", "google");
val = link.hasAttribute("title");
link.removeAttribute("title");
val = link;

console.log(val);

'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글

Key & Input events  (0) 2021.05.31
Mouse events  (0) 2021.05.31
Creating Elements  (0) 2021.05.31
traversing dom  (0) 2021.05.30
Multiple Selector  (0) 2021.05.30
Comments