You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*map*:Iterate an array elements and modify the array elements. It takes a callback function with elements and index parameter and return the modified array.
1400
+
*map*:Iterate an array elements and modify the array elements. It takes a callback function with elements and index parameter and return the modified array.
*Filter*:Screen out the itmes which full fill screening requirments
1442
+
*Filter*: Filter out itmes which full fill filtering conditions
1443
1443
1444
1444
```js
1445
1445
//Filter countries containing land
@@ -1535,7 +1535,7 @@ console.log(age) // 5
1535
1535
1. Find out with which letter are there many countries
1536
1536
1537
1537
## Document Object Model
1538
-
HTML document is structured as a JavaScript Object. Every HTML element has a different properties which can help to manipulate it. It is possible get, create, append or remove HTML elements using JavaScript. Check the examples below. Selecting HTML element using JavaScript is similar to select CSS. To select an HTML element, we use tag name, id, class name. To create an HTML element we use tag name.
1538
+
HTML document is structured as a JavaScript Object. Every HTML element has a different properties which can help to manipulate it. It is possible to get, create, append or remove HTML elements using JavaScript. Check the examples below. Selecting HTML element using JavaScript is similar to select CSS. To select an HTML element, we use tag name, id, class name. To create an HTML element we use tag name.
1539
1539
1540
1540
### Getting Element
1541
1541
```html
@@ -1563,8 +1563,8 @@ for(let i = 0; i < allTitles.length; i++){
1563
1563
}
1564
1564
1565
1565
```
1566
-
#### Getting elements by tag class name
1567
-
*getElementsByClassName()* method returns an HTMLCollection object. An HTMLCollection is an array like list of HTML elements. The length property provides the size of the collection.
1566
+
#### Getting elements by class name
1567
+
*getElementsByClassName()* method returns an HTMLCollection object. An HTMLCollection is an array like list of HTML elements. The length property provides the size of the collection. It is possible to loop through all the HTMLCollection elements. See the example below
@@ -1574,21 +1574,21 @@ for(let i = 0; i < allTitles.length; i++){
1574
1574
}
1575
1575
```
1576
1576
#### Getting an element by id
1577
-
*getElementsById()* method returns an HTML element.
1577
+
*getElementsById()* targets a single HTML element. We pass the id without # as an argument.
1578
1578
```js
1579
1579
let firstTitle =document.getElementById("first-title");
1580
1580
console.log(firstTitle) // <h1>First Title</h1>
1581
1581
```
1582
1582
#### Getting elements by using querySelector using tag, class and id:
1583
-
*querySelector*: can be used to select html element by its tag name, id or class. If the tag name is used it selects only the first element
1583
+
*querySelector*: can be used to select HTML element by its tag name, id or class. If the tag name is used it selects only the first element.
1584
1584
1585
1585
```js
1586
1586
let firstTitle =document.querySelect("h1");// select the first available h2 element
1587
1587
let firstTitle =document.querySelector("#first-title"); // select id with first-title
1588
1588
let firstTitle =document.querySelector(".title"); // select the first available h2 element with class title
1589
1589
1590
1590
```
1591
-
*querySelectorAll*: can be used to select html element by its tag name or class. It return a nodeList which is an array like object which support array methods.
1591
+
*querySelectorAll*: can be used to select html element by its tag name or class. It return a nodeList which is an array like object which support array methods. We can use *for loop* or *forEach* to loop through each nodeList elements.
1592
1592
```js
1593
1593
constallTitles=document.querySelectAll("h1");
1594
1594
console.log(allTitles.length) // 4
@@ -1601,17 +1601,27 @@ const allTitles = document.querySelectorAll(".title"); // the same goes for sele
1601
1601
### Adding attribute
1602
1602
An attribute is added in the opening tag of HTML which gives additional information about the element. Common HTML attributes: id, class, src, style, href,disabled, title, alt. Lets add id and class for the fourth title.
1603
1603
1604
+
#### Adding attribute using setAttribute
1605
+
The *setAttribute()* method set any html attribute. It takes two parameters the type of the attribute and the name of the attribute.
1606
+
Let's add class and id attribute for the fourth title.
1607
+
1604
1608
```js
1605
1609
consttitles=document.querySelectorAll("h1");
1606
1610
titles[3].setAttribut("class", "title");
1607
1611
titles[3].setAttribut("id", "fourth-title");
1612
+
```
1613
+
#### Adding attribute without setAttribute
1614
+
Some attributes are DOM object property and they can be set directly. For instance id and class
1615
+
```js
1608
1616
//another way to setting an attribute
1609
1617
titles[3].className="title";
1610
1618
titles[3].id="fourth-title";
1611
-
1619
+
```
1620
+
#### Adding class using classList
1621
+
The class list method is a good method to append additional class. It doens't override the original class if a class exists
1622
+
```js
1612
1623
//another way to setting an attribute: append the class, does't over ride
1613
1624
titles[3].classList.add("title", "header-title")
1614
-
1615
1625
```
1616
1626
### Adding Text conent
1617
1627
```js
@@ -1672,9 +1682,43 @@ button.addEventListener("click", e => {
1672
1682
console.log(e.target);
1673
1683
});
1674
1684
```
1685
+
### Getting value from an input element
1686
+
We usaully fill forms and forms accept data. Form fields are created using input HTML element.
1687
+
```html
1688
+
<input type ="text" placeholder ="Mass in Killogram"/>
1689
+
<input type ="text" placeholder ="Height in meters"/>
1690
+
<button>Calculate BMI</button>
1691
+
```
1692
+
```js
1693
+
constmass=document.querySelector('#mass');
1694
+
constheight=document.querySelector('#height');
1695
+
constbutton=document.querySelector('button');
1696
+
let bmi;
1697
+
button.addEventListen('click', ()=>{
1698
+
bmi =mass.value*height.value;
1699
+
});
1700
+
1701
+
console.log(bmi)
1675
1702
1676
-
#### Exercises:Document Object Model
1677
1703
1704
+
```
1705
+
1706
+
#### Exercises:Document Object Model
1707
+
```html
1708
+
<!--index.html-->
1709
+
<p>First Paragraph</p>
1710
+
<p>Second Paragraph</p>
1711
+
<p>Third Paragraph</p>
1712
+
<p>Fourth Paragraph</p>
1713
+
```
1714
+
1. Create an index.html file and put four p elementts as above:Get the first paragraph, get all paragraph
1715
+
2. Set id and class attribute for all the paragraphs using different attribute setting methods
1716
+
3. Change stye of each paragraph using JavaScript(eg. color, background, border, font-size, font-famil)
1717
+
4. Select all paragraphs and loop through each elements and give the first and third paragraph a color of color, and the second and the fourth paragraph a red color
1718
+
5. Create a div container on HTML document and create 100 numbers dynamically and append to the container div. Put each number in 150px by 150px box. If the number is even the background will be lightgreen else lightblue.
1719
+
6. Use the rgb color generator function or hexaColor generator to create 10 divs with random background colors
0 commit comments