Javascript Day-4
Javascript Day-4
.................................................
JSON.stringify:
ex:
ex:
.......................................................
Post Data:
html:
js:
function submitPost() {
var title = document.getElementById('titleInput').value;
var userId = document.getElementById('userIdInput').value;
................................................
DOM:
The Document Object Model (DOM) is a programming interface for web documents. It
represents the structure of an HTML or XML document as a tree-like structure, where
each node represents an element, attribute, or piece of text in the document.
console.log(window);
console.log(this);
console.log(window.location);
....................................
console.log(window.document);
(or)
console.log(document);
................................................
html:
1) getElementById:
2) getElementsByClassName:
3) getElementsByTagName:
This method selects elements based on their HTML tag name.
const elements = document.getElementsByTagName('h1');
4) querySelector:
This method selects the first element that matches a CSS selector.
const element = document.querySelector('#myDiv.myClass');
5) querySelectorAll:
This method selects all elements that match a CSS selector and returns them as a
NodeList (or an array-like object).
.............................................................
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<style>
.red {
color: red;
}
.bold {
font-weight: bold;
}
</style>
</head>
<body>
<h1 id="title">Hello, World!</h1>
<p class="myClass">Paragraph 1</p>
<p class="myClass">Paragraph 2</p>
<div>
<span>Span 1</span>
<span>Span 2</span>
</div>
<script src="./index.js"></script>
</body>
</html>
js File:
.................................................................
Form Validation:
Html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<style>
.error {
color: red;
}
</style>
</head>
<body>
<h1>Form Validation Example</h1>
<form id="myForm" onsubmit="validateForm(event)">
<label for="name">Name:</label>
<input type="text" id="name" required>
<span id="nameError" class="error"></span>
<br>
<label for="email">Email:</label>
<input type="email" id="email" required>
<span id="emailError" class="error"></span>
<br>
<input type="submit" value="Submit">
</form>
<script src="./index.js"></script>
</body>
</html>
JS File:
function validateForm(event) {
event.preventDefault(); // Prevent form submission
// Perform validation
let isValid = true;
if (isValid) {
// Form is valid, submit or perform further actions
console.log('Form submitted');
// Here, you can submit the form using AJAX or perform any other desired
actions
}
}
.............................................................
Event Listners:
In JavaScript, event listeners are functions that wait for specific events to occur
on an HTML element, allowing you to respond to user interactions or other actions.
Here are some commonly used types of event listeners in JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<style>
.active {
color: red;
}
</style>
</head>
<body>
<h1 id="myHeading">Click Me!</h1>
<script src="./index.js"></script>
</body>
</html>
js File:
Event bubbling :
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<style>
#outer {
border: 2px solid blue;
padding: 10px;
}
#inner {
border: 2px solid green;
padding: 10px;
}
</style>
</head>
<body>
<div id="outer">
<div id="inner">
<button id="button">Click Me!</button>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
Js File:
inner.addEventListener('click', function(event) {
console.log('Inner div clicked');
});
button.addEventListener('click', function(event) {
console.log('Button clicked');
});
...........................................................
Event Capturing:
Event capturing is a phase in the event handling process where events are detected
and triggered starting from the top-most ancestor element and propagating down the
DOM hierarchy to the target element.
Html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<style>
#outer {
border: 2px solid blue;
padding: 10px;
}
#inner {
border: 2px solid green;
padding: 10px;
}
</style>
</head>
<body>
<div id="outer">
<div id="inner">
<button id="button">Click Me!</button>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
JS File:
button.addEventListener('click', function(event) {
console.log('Button clicked');
});
....................................................
stopPropagation() :
The stopPropagation() method is a built-in function in JavaScript that allows you
to stop the further propagation of an event through the DOM hierarchy. It is used
to prevent event bubbling or event capturing from continuing beyond the current
element.
Js file :
const outer = document.getElementById('outer');
const inner = document.getElementById('inner');
const button = document.getElementById('button');
inner.addEventListener('click', function(event) {
console.log('Inner div clicked');
event.stopPropagation(); // Stop event propagation
});
button.addEventListener('click', function(event) {
console.log('Button clicked');
});
...................................................................
async and await are keywords used to work with asynchronous operations in a more
synchronous-like manner.
They are part of the ES2017 (ES8) language specification and are commonly used in
modern JavaScript code.
await: The await keyword can only be used inside an async function. It is used to
pause the execution of the function until a Promise is resolved or rejected. When
await is used with a Promise, it waits for the Promise to settle and then returns
the resolved value. Here's an example of using await:
fetchData();