Java Script
Java Script
Java Script
STD :- FY(BSC)IT
ROLL NO :- 24
SUBJECT :- WEBAPPLICATION
DEVELOPMENT
1
2
2
3
3
4
4
5
DOM Manipulation:
The Document Object Model (DOM) represents the
structure of an HTML document as a tree of objects.
JavaScript can manipulate the DOM to add, remove, or
modify HTML elements, attributes, and styles.
DOM methods like get Element ById query Selector,
append Child, and add Event Listener are commonly
used for DOM manipulation.
Event Handling:-
Events are actions or occurrences that happen in the
browser, such as clicking a button or submitting a form.
5
6
Asynchronous Programming:-
Asynchronous programming allows JavaScript to
execute multiple tasks simultaneously without blocking
the main thread.
Callbacks, promises, and async/await are common
techniques used for asynchronous programming.
Asynchronous tasks include fetching data from servers,
waiting for user input, and performing animation
6
7
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("John")); // Output: Hello, John!
Loops:-
for (var i = 0; i < 5; i++) {
console.log(i);
}
1. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
7
8
<title>To-Do List</title>
</head>
<body>
<h1>To-Do List</h1>
<form id="taskForm">
<input type="text" id="taskInput"
placeholder="Enter task...">
<button type="submit">Add Task</button>
</form>
<ul id="taskList"></ul>
<script>
document. GetElement
ById('taskForm') .addEventListener('submit',
function(event) {
event.preventDefault(); // Prevent form submission
var taskInput =
document.getElementById('taskInput').value;
if (taskInput.trim() !== '') {
addTask(taskInput); // Call addTask function
8
9
document.getElementById('taskInput').value = ''; //
Clear input field
}
});
function addTask(taskText) {
var taskList = document.getElementById('taskList');
var li = document.createElement('li');
li.textContent = taskText;
taskList.appendChild(li);
}
</script>
</body>
</html>
2. Implement functionality to mark tasks as completed
when they are clicked:
document.getElementById('taskList').addEventListener(
'click', function(event) {
if (event.target.tagName === 'LI') {
event.target.classList.toggle('completed'); // Toggle
completed class
9
10
}
});
3.document.getElementById('taskList').addEventListene
r('click', function(event) {
if (event.target.tagName === 'BUTTON') {
event.target.parentElement.remove(); // Remove
parent element (li) of the clicked button
}
});
Explanation:
Adding Tasks: We listen for the form submission event
and prevent the default form submission behavior.
Then, we get the value of the task input, check if it's
not empty, and call the addTask function to add the
task to the list.
10
11
11