pull/66/head
Asabeneh 5 years ago
parent 4bc164d2ad
commit 12f1aa06c3

1
.gitignore vendored

@ -14,7 +14,6 @@ day9.md
day10.md
01_02_03_days_backup.md
test.md
22_Day
23_Day
24_Day
25_Day

@ -33,6 +33,8 @@
- [Adding Text to HTML element](#adding-text-to-html-element)
- [Adding Text content using textContent](#adding-text-content-using-textcontent)
- [Adding Text Content using innHTML](#adding-text-content-using-innhtml)
- [Text Content](#text-content)
- [Inner HTML](#inner-html)
- [Adding style](#adding-style)
- [Adding Style Color](#adding-style-color)
- [Adding Style Background Color](#adding-style-background-color)
@ -216,11 +218,78 @@ titles[3].textContent = 'Fourth Title'
Most people get confused between _textContent_ and _innerHTML_. _textContent_ is meant to add text to an HTML element, however innerHTML can add a text or HTML element or elements as a child.
##### Text Content
We assign *textContent* HTML object property to a text
```js
const titles = document.querySelectorAll('h1')
titles[3].textContent = 'Fourth Title'
```
##### Inner HTML
We use innerHTML property when we like to replace or a completely new children content to a parent element.
It value we assign is going to be a string of HTML elements.
```html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript for Everyone:DOM</title>
</head>
<body>
<div class="wrapper">
<h1>Asabeneh Yetayeh challenges in 2020</h1>
<h2>30DaysOfJavaScript Challenge</h2>
<ul></ul>
</div>
<script>
const lists = `
<li>30DaysOfPython Challenge Done</li>
<li>30DaysOfJavaScript Challenge Ongoing</li>
<li>30DaysOfReact Challenge Coming</li>
<li>30DaysOfFullStack Challenge Coming</li>
<li>30DaysOfDataAnalysis Challenge Coming</li>
<li>30DaysOfReactNative Challenge Coming</li>
<li>30DaysOfMachineLearning Challenge Coming</li>`
const ul = document.querySelector('ul')
ul.innerHTML = lists
</script>
</body>
</html>
```
The innerHTML property can allow us also to remove all the children of a parent element. Instead of using removeChild() I would recommend the following method.
```html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript for Everyone:DOM</title>
</head>
<body>
<div class="wrapper">
<h1>Asabeneh Yetayeh challenges in 2020</h1>
<h2>30DaysOfJavaScript Challenge</h2>
<ul>
<li>30DaysOfPython Challenge Done</li>
<li>30DaysOfJavaScript Challenge Ongoing</li>
<li>30DaysOfReact Challenge Coming</li>
<li>30DaysOfFullStack Challenge Coming</li>
<li>30DaysOfDataAnalysis Challenge Coming</li>
<li>30DaysOfReactNative Challenge Coming</li>
<li>30DaysOfMachineLearning Challenge Coming</li>
</ul>
</div>
<script>
const ul = document.querySelector('ul')
ul.innerHTML = ''
</script>
</body>
</html>
```
### Adding style
#### Adding Style Color

@ -0,0 +1,230 @@
<div align="center">
<h1> 30 Days Of JavaScript</h1>
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
</a>
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a>
<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> January, 2020</small>
</sub>
</div>
[<< Day 21](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/21_Day/21_day_dom.md) | [Day 23 >>]()
![Thirty Days Of JavaScript](../images/banners/day_1_22.png)
- [Day 22](#day-22)
- [DOM(Document Object Model)-Day 2](#domdocument-object-model-day-2)
- [Creating an Element](#creating-an-element)
- [Creating elements](#creating-elements)
- [Appending child to a parent element](#appending-child-to-a-parent-element)
- [Removing a child element from a parent node](#removing-a-child-element-from-a-parent-node)
- [Exercises](#exercises)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3)
# Day 22
## DOM(Document Object Model)-Day 2
### Creating an Element
To create an HTML element we use tag name. Creating an HTML element using JavaScript is very simple and straight forward. We use the method _document.createElement()_. The method takes an HTML element tag name as a string parameter.
```js
// syntax
document.createElement('tagname')
```
```html
<!DOCTYPE html>
<html>
<head>
<title>Document Object Model:30 Days Of JavaScript</title>
</head>
<body>
<script>
let title = document.createElement('h1')
title.className = 'title'
title.style.fontSize = '24px'
title.textContent = 'Creating HTML element DOM Day 2'
console.log(title)
</script>
</body>
</html>
```
### Creating elements
To create multiple elements we should use loop. Using loop we can create as many HTML elements as we want.
After we create the element we can assign value to the different properties of the HTML object.
```html
<!DOCTYPE html>
<html>
<head>
<title>Document Object Model:30 Days Of JavaScript</title>
</head>
<body>
<script>
let title
for (let i = 0; i < 3; i++) {
title = document.createElement('h1')
title.className = 'title'
title.style.fontSize = '24px'
title.textContent = i
console.log(title)
}
</script>
</body>
</html>
```
### Appending child to a parent element
To see a created element on the HTML document we should append it to the parent as a child element. We can access the HTML document body using *document.body*. The *document.body* support the *appendChild()* method. See the example below.
```html
<!DOCTYPE html>
<html>
<head>
<title>Document Object Model:30 Days Of JavaScript</title>
</head>
<body>
<script>
// creating multiple elements and appending to parent element
let title
for (let i = 0; i < 3; i++) {
title = document.createElement('h1')
title.className = 'title'
title.style.fontSize = '24px'
document.body.appendChild(title)
}
</script>
</body>
</html>
```
### Removing a child element from a parent node
After creating an HTML, we may want to remove element or elements and we can use the *removeChild()* method.
**Example:**
```html
<!DOCTYPE html>
<html>
<head>
<title>Document Object Model:30 Days Of JavaScript</title>
</head>
<body>
<h1>Removing child Node</h1>
<h2>Asabeneh Yetayeh challenges in 2020</h1>
<ul>
<li>30DaysOfPython Challenge Done</li>
<li>30DaysOfJavaScript Challenge Done</li>
<li>30DaysOfReact Challenge Coming</li>
<li>30DaysOfFullStack Challenge Coming</li>
<li>30DaysOfDataAnalysis Challenge Coming</li>
<li>30DaysOfReactNative Challenge Coming</li>
<li>30DaysOfMachineLearning Challenge Coming</li>
</ul>
<script>
const ul = document.querySelector('ul')
const lists = document.querySelectorAll('li')
for (const list of lists) {
ul.removeChild(list)
}
</script>
</body>
</html>
```
As we have see in the previous section there is a better way to eliminate all the inner HTML elements or the children of a parent element using the method *innerHTML* properties.
```html
<!DOCTYPE html>
<html>
<head>
<title>Document Object Model:30 Days Of JavaScript</title>
</head>
<body>
<h1>Removing child Node</h1>
<h2>Asabeneh Yetayeh challenges in 2020</h1>
<ul>
<li>30DaysOfPython Challenge Done</li>
<li>30DaysOfJavaScript Challenge Done</li>
<li>30DaysOfReact Challenge Coming</li>
<li>30DaysOfFullStack Challenge Coming</li>
<li>30DaysOfDataAnalysis Challenge Coming</li>
<li>30DaysOfReactNative Challenge Coming</li>
<li>30DaysOfMachineLearning Challenge Coming</li>
</ul>
<script>
const ul = document.querySelector('ul')
ul.innerHTML = ''
</script>
</body>
</html>
```
The above snippet of code cleared all the child elements.
---
🌕 You are so special, you are progressing everyday. Now, you knew how to destroy a created DOM element when it is needed. You learned DOM and now you have the capability to build and develop applications. You are left with only eight days to your way to greatness. Now do some exercises for your brain and for your muscle.
## Exercises
### Exercises: Level 1
1. Create a div container on HTML document and create 100 to 100 numbers dynamically and append to the container div.
- Even numbers background is green
- Odd numbers background is yellow
- Prime numbers background is red
![Number Generator](./../images/projects/dom_min_project_day_number_generators_2.1.png)
### Exercises: Level 2
1. Use the countries array to display all the countries.See the design
![World Countries List](./../images/projects/dom_min_project_countries_aray_day_2.2.png)
### Exercises: Level 3
Check the requirement of this project from both images(jpg and gif). All the data and CSS has been implemented using JavaScript only. The data is found on starter folder project_3.
![Challenge Information](./22_day_starter/design/dom_mini_project_challenge_info_day_2.3.png)
![Challenge Information](./22_day_starter/design/dom_mini_project_challenge_info_day_2.3.gif)
🎉 CONGRATULATIONS ! 🎉
[<< Day 21](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/21_Day/21_day_dom.md) | [Day 23 >>]()

Binary file not shown.

After

Width:  |  Height:  |  Size: 866 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 KiB

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfJavaScript:22 Day: Number Generator </title>
</head>
<body>
<h1>Number Generator</h1>
<h2>30DaysOfJavaScript:DOM Day 2</h2>
<h3>Author: Asabeneh Yetayeh</h3>
<div class="wrapper">
</div>
<script src="./scripts/main.js"></script>
</body>
</html>

@ -0,0 +1,2 @@
console.log(countries)
alert('Open the console and check if the countries has been loaded')

@ -0,0 +1,195 @@
const countries = [
'Afghanistan',
'Albania',
'Algeria',
'Andorra',
'Angola',
'Antigua and Barbuda',
'Argentina',
'Armenia',
'Australia',
'Austria',
'Azerbaijan',
'Bahamas',
'Bahrain',
'Bangladesh',
'Barbados',
'Belarus',
'Belgium',
'Belize',
'Benin',
'Bhutan',
'Bolivia',
'Bosnia and Herzegovina',
'Botswana',
'Brazil',
'Brunei',
'Bulgaria',
'Burkina Faso',
'Burundi',
'Cambodia',
'Cameroon',
'Canada',
'Cape Verde',
'Central African Republic',
'Chad',
'Chile',
'China',
'Colombi',
'Comoros',
'Congo (Brazzaville)',
'Congo',
'Costa Rica',
"Cote d'Ivoire",
'Croatia',
'Cuba',
'Cyprus',
'Czech Republic',
'Denmark',
'Djibouti',
'Dominica',
'Dominican Republic',
'East Timor (Timor Timur)',
'Ecuador',
'Egypt',
'El Salvador',
'Equatorial Guinea',
'Eritrea',
'Estonia',
'Ethiopia',
'Fiji',
'Finland',
'France',
'Gabon',
'Gambia, The',
'Georgia',
'Germany',
'Ghana',
'Greece',
'Grenada',
'Guatemala',
'Guinea',
'Guinea-Bissau',
'Guyana',
'Haiti',
'Honduras',
'Hungary',
'Iceland',
'India',
'Indonesia',
'Iran',
'Iraq',
'Ireland',
'Israel',
'Italy',
'Jamaica',
'Japan',
'Jordan',
'Kazakhstan',
'Kenya',
'Kiribati',
'Korea, North',
'Korea, South',
'Kuwait',
'Kyrgyzstan',
'Laos',
'Latvia',
'Lebanon',
'Lesotho',
'Liberia',
'Libya',
'Liechtenstein',
'Lithuania',
'Luxembourg',
'Macedonia',
'Madagascar',
'Malawi',
'Malaysia',
'Maldives',
'Mali',
'Malta',
'Marshall Islands',
'Mauritania',
'Mauritius',
'Mexico',
'Micronesia',
'Moldova',
'Monaco',
'Mongolia',
'Morocco',
'Mozambique',
'Myanmar',
'Namibia',
'Nauru',
'Nepal',
'Netherlands',
'New Zealand',
'Nicaragua',
'Niger',
'Nigeria',
'Norway',
'Oman',
'Pakistan',
'Palau',
'Panama',
'Papua New Guinea',
'Paraguay',
'Peru',
'Philippines',
'Poland',
'Portugal',
'Qatar',
'Romania',
'Russia',
'Rwanda',
'Saint Kitts and Nevis',
'Saint Lucia',
'Saint Vincent',
'Samoa',
'San Marino',
'Sao Tome and Principe',
'Saudi Arabia',
'Senegal',
'Serbia and Montenegro',
'Seychelles',
'Sierra Leone',
'Singapore',
'Slovakia',
'Slovenia',
'Solomon Islands',
'Somalia',
'South Africa',
'Spain',
'Sri Lanka',
'Sudan',
'Suriname',
'Swaziland',
'Sweden',
'Switzerland',
'Syria',
'Taiwan',
'Tajikistan',
'Tanzania',
'Thailand',
'Togo',
'Tonga',
'Trinidad and Tobago',
'Tunisia',
'Turkey',
'Turkmenistan',
'Tuvalu',
'Uganda',
'Ukraine',
'United Arab Emirates',
'United Kingdom',
'United States',
'Uruguay',
'Uzbekistan',
'Vanuatu',
'Vatican City',
'Venezuela',
'Vietnam',
'Yemen',
'Zambia',
'Zimbabwe'
]

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfJavaScript:22 Day: World Countries List</title>
</head>
<body>
<header>
<h2>World Countries List</h2>
<h4 id="total-countries"></h4>
<h3>30DaysOfJavaScript:DOM-Day-2</h3>
<h3>Author: Asabeneh Yetayeh</h3>
</header>
<div class="countries-container">
<div class="countries-wrapper">
</div>
</div>
<script src="./data/countries.js"></script>
<script src="./js/script.js"></script>
</body>
</html>
</html>

@ -0,0 +1,195 @@
const asabenehChallenges2020 = {
description: 'Asabeneh Yetayeh challenges',
challengeTitle: 'Asabeneh Yetayeh challenges',
challengeSubtitle: '30DaysOfJavaScript Challenge',
challengeYear: 2020,
keywords: [
'HTML',
'HTML5',
'CSS',
'CSS3',
'JS',
'JavaScript',
'ES6',
'Promise',
'async await',
'Database',
'React',
'React Hooks',
'Context API',
'React Router',
'Redux',
'Node',
'MongoDB',
'SQL',
'API',
'DOM',
'data science',
'MERN',
'Python',
'Flask',
'Statistics',
'Linear Algebra',
'Numpy',
'Pandas',
'Scipy',
'Scikit-learn',
'Visualization',
'D3.js'
],
author: {
firstName: 'Asabeneh',
lastName: 'Yetayeh',
titles: [
['🌱', 'Educator'],
['💻', 'Programmer'],
['🌐', 'Developer'],
['🔥', 'Motivator'],
['📔', 'Content Creator']
],
qualifications: [
'MSc. Computer Science Ongoing',
'BSc. Information and Communication Eng.',
'MSc. Food Technology',
'BSc.Food Technology'
],
socialLinks: [
{
social: 'LinkedIn',
url: 'https://www.linkedin.com/in/asabeneh/',
fontawesomeIcon: '<i class="fab fa-linkedin">'
},
{
social: 'Twitter',
url: 'https://twitter.com/Asabeneh',
fontawesomeIcon: '<i class="fab fa-twitter-square"></i>'
},
{
social: 'Github',
fontawesomeIcon: '<i class="fab fa-github-square"></i>',
url: 'https://github.com/Asabeneh'
},
{
social: 'DEV.to',
fontawesomeIcon: '',
url: 'https://dev.to/asabeneh'
}
],
skills: [
'Web Development',
'Data Analysis',
'Data Visualization',
'Programming',
'Databases',
'Developing API'
],
bio:
'I am an educator, developer, motivator and content creator. I am a life-long learner. If you like to know more about me checkout my LinkedIn or Github profile. Thank you so much for joining in my quest of changing everyone to developer.'
},
challenges: [
{
name: '30 Days Of Python',
topics: [
'Python',
'Flask',
'Numpy',
'Pandas',
'Statistics',
'API',
'MongoDB'
],
days: 30,
status: 'Done',
questions: 'Above 500',
projects: 'Two',
interviewQns: '',
githubUrl: 'https://github.com/Asabeneh/30-Days-Of-Python'
},
{
name: '30 Days Of JavaScript',
topics: ['JavaScript', 'ES6', 'Promise', 'async and await', 'DOM'],
days: 30,
status: 'Ongoing',
questions: 'Above 500',
projects: 'About 30',
interviewQns: '',
githubUrl: 'https://github.com/Asabeneh/30DaysOfJavaScript'
},
{
name: '30 Days Of HTML & CSS',
topics: ['CSS', 'Flex', 'Grid', 'CSS Animation'],
days: 30,
status: 'Coming',
questions: 'Above 500',
projects: 'Two',
interviewQns: '',
githubUrl: ''
},
{
name: '30 Days Of React',
topics: [
'React',
'React Router',
'Redux',
'Context API',
'React Hooks',
'MERN'
],
days: 30,
status: 'Coming',
questions: '',
projects: '',
interviewQns: '',
githubUrl: ''
},
{
name: '30 Days Of ReactNative',
topics: ['ReactNative', 'Redux'],
days: 30,
status: 'Coming',
questions: '',
projects: 'Two',
interviewQns: '',
githubUrl: ''
},
{
name: '30 Days Of Fullstack',
topics: ['React', 'Redux', 'MongoDB', 'Node', 'MERN'],
days: 30,
status: 'Coming',
questions: '',
projects: '',
interviewQns: '',
githubUrl: ''
},
{
name: '30 Days Of Data Analysis',
topics: ['Python', 'Numpy', 'Pandas', 'Statistics', 'Visualization'],
days: 30,
status: 'Coming',
questions: '',
projects: '',
interviewQns: '',
githubUrl: ''
},
{
name: '30 Days Of Machine Learning',
topics: [
'Python',
'Numpy',
'Pandas',
'Scikit-learn',
'Scipy',
'Linear Algebra',
'Statistics',
'Visualization'
],
days: 30,
status: 'Coming',
questions: '',
projects: '',
interviewQns: '',
githubUrl: ''
}
]
}

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfJavaScript:22 Day: Challenge Info</title>
</head>
<body>
<div class="wrapper">
</div>
<script src="./data/challenges_info.js"></script>
<script src="./js/script.js"></script>
</body>
</html>
</html>

@ -0,0 +1 @@
console.log(asabenehChallenges2020)

@ -0,0 +1,195 @@
const countries = [
'Afghanistan',
'Albania',
'Algeria',
'Andorra',
'Angola',
'Antigua and Barbuda',
'Argentina',
'Armenia',
'Australia',
'Austria',
'Azerbaijan',
'Bahamas',
'Bahrain',
'Bangladesh',
'Barbados',
'Belarus',
'Belgium',
'Belize',
'Benin',
'Bhutan',
'Bolivia',
'Bosnia and Herzegovina',
'Botswana',
'Brazil',
'Brunei',
'Bulgaria',
'Burkina Faso',
'Burundi',
'Cambodia',
'Cameroon',
'Canada',
'Cape Verde',
'Central African Republic',
'Chad',
'Chile',
'China',
'Colombi',
'Comoros',
'Congo (Brazzaville)',
'Congo',
'Costa Rica',
"Cote d'Ivoire",
'Croatia',
'Cuba',
'Cyprus',
'Czech Republic',
'Denmark',
'Djibouti',
'Dominica',
'Dominican Republic',
'East Timor (Timor Timur)',
'Ecuador',
'Egypt',
'El Salvador',
'Equatorial Guinea',
'Eritrea',
'Estonia',
'Ethiopia',
'Fiji',
'Finland',
'France',
'Gabon',
'Gambia, The',
'Georgia',
'Germany',
'Ghana',
'Greece',
'Grenada',
'Guatemala',
'Guinea',
'Guinea-Bissau',
'Guyana',
'Haiti',
'Honduras',
'Hungary',
'Iceland',
'India',
'Indonesia',
'Iran',
'Iraq',
'Ireland',
'Israel',
'Italy',
'Jamaica',
'Japan',
'Jordan',
'Kazakhstan',
'Kenya',
'Kiribati',
'Korea, North',
'Korea, South',
'Kuwait',
'Kyrgyzstan',
'Laos',
'Latvia',
'Lebanon',
'Lesotho',
'Liberia',
'Libya',
'Liechtenstein',
'Lithuania',
'Luxembourg',
'Macedonia',
'Madagascar',
'Malawi',
'Malaysia',
'Maldives',
'Mali',
'Malta',
'Marshall Islands',
'Mauritania',
'Mauritius',
'Mexico',
'Micronesia',
'Moldova',
'Monaco',
'Mongolia',
'Morocco',
'Mozambique',
'Myanmar',
'Namibia',
'Nauru',
'Nepal',
'Netherlands',
'New Zealand',
'Nicaragua',
'Niger',
'Nigeria',
'Norway',
'Oman',
'Pakistan',
'Palau',
'Panama',
'Papua New Guinea',
'Paraguay',
'Peru',
'Philippines',
'Poland',
'Portugal',
'Qatar',
'Romania',
'Russia',
'Rwanda',
'Saint Kitts and Nevis',
'Saint Lucia',
'Saint Vincent',
'Samoa',
'San Marino',
'Sao Tome and Principe',
'Saudi Arabia',
'Senegal',
'Serbia and Montenegro',
'Seychelles',
'Sierra Leone',
'Singapore',
'Slovakia',
'Slovenia',
'Solomon Islands',
'Somalia',
'South Africa',
'Spain',
'Sri Lanka',
'Sudan',
'Suriname',
'Swaziland',
'Sweden',
'Switzerland',
'Syria',
'Taiwan',
'Tajikistan',
'Tanzania',
'Thailand',
'Togo',
'Tonga',
'Trinidad and Tobago',
'Tunisia',
'Turkey',
'Turkmenistan',
'Tuvalu',
'Uganda',
'Ukraine',
'United Arab Emirates',
'United Kingdom',
'United States',
'Uruguay',
'Uzbekistan',
'Vanuatu',
'Vatican City',
'Venezuela',
'Vietnam',
'Yemen',
'Zambia',
'Zimbabwe'
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 866 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 KiB

@ -543,7 +543,7 @@ let country = 'Finland' // country
let city = 'Helsinki' // capital city
let isMarried = true // boolean data type
console.log(firstName, lastName, country, city, age, isMarried)
console.log(firstName, lastName, country, city, isMarried)
```
```sh

Loading…
Cancel
Save