Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 4_DOM.md #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions 06_dom/4_DOM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# JavaScript DOM Manipulation 4

## 1. Adding Items to a List

### 1.1. Basic Function to Add a Language

```javascript
function addLanguage(langName) {
// Create a new list item element

const li = document.createElement("li");

// Set the list item content to the language name

li.innerHTML = langName;

// Add the list item to the .language list

document.querySelector(".language").appendChild(li);
}

// Example: Adding "Python" to the list

addLanguage("Python");

```

### 1.2. Optimized Function to Add a Language

```javascript
function addLanguageInOptimizeWay(langName) {
// Create a new list item element

const li = document.createElement("li");

// Create a text node with the language name and add it to the list item

li.appendChild(document.createTextNode(langName));

// Add the list item to the .language list

document.querySelector(".language").appendChild(li);
}

// Example: Adding "C++" and "Java" to the list

addLanguageInOptimizeWay("C++");
addLanguageInOptimizeWay("Java");

```
## 2. Editing List Items

### 2.1. Edit Using `innerHTML`
- Note : This directly changes the content of the selected list item.

```javascript

// Select the second list item

const secondLang = document.querySelector("li:nth-child(2)");

// Change its content to "Ruby"

secondLang.innerHTML = "Ruby";

```

### 2.2. Edit by `Replacing the Element`

- Note: This approach creates a new element and replaces the old one, rather than just changing the content.

```javascript

// Create a new list item element

const newList = document.createElement('li');
newList.textContent = "Ruby";

// Replace the old second list item with the new one

const secondLang = document.querySelector("li:nth-child(2)");
secondLang.replaceWith(newList);

```

### 2.3. Edit Using `outerHTML`
- Note : This completely replaces the selected list item, including the HTML structure.

```javascript

// Replace the entire first list item with a new one

const firstLang = document.querySelector("li:first-child");
firstLang.outerHTML = "<li>TypeScript</li>";

```

## 3. Removing the last list item
- Note : This removes the selected list item from the list.

```javascript

// Select the last list item and remove it

const lastLang = document.querySelector('li:last-child');
lastLang.remove();

```