-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
92 lines (83 loc) · 3.07 KB
/
index.html
File metadata and controls
92 lines (83 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cricket Score Board</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="form-container">
<h1 class="header">Cricket Score Board</h1>
<form onsubmit="addDetailsToTable(event)">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter name" required><br>
<label for="balls">Balls faced:</label>
<input type="number" id="balls" name="balls" placeholder="Enter balls faced" required><br>
<label for="runs">Runs scored:</label>
<input type="number" id="runs" name="runs" placeholder="Enter runs scored" required><br>
<div class="btn-container">
<button type="submit" class="add-button" id="add">Add</button>
<button type="button" class="remove-button" id="remove" onclick="removeSelectedRow()">Remove</button>
</div>
</form>
</div>
<div class="tables">
<table id="details">
<tr>
<th>Name</th>
<th>Balls faced</th>
<th>Runs scored</th>
<th>Strike rate</th>
</tr>
</table>
</div>
<script>
let selectedRow = null;
function addDetailsToTable(event) {
event.preventDefault();
const name = document.getElementById('name').value;
const balls = document.getElementById('balls').value;
const runs = document.getElementById('runs').value;
const table = document.getElementById('details');
if (selectedRow) {
selectedRow.cells[0].textContent = name;
selectedRow.cells[1].textContent = balls;
selectedRow.cells[2].textContent = runs;
let strikerate = balls > 0 ? (runs * 100) / balls : 0;
selectedRow.cells[3].textContent = strikerate.toFixed(2);
selectedRow = null;
} else {
const newRow = table.insertRow();
newRow.addEventListener('click', () => selectRow(newRow));
const nameCell = newRow.insertCell(0);
const ballsCell = newRow.insertCell(1);
const runsCell = newRow.insertCell(2);
const strikeRateCell = newRow.insertCell(3);
nameCell.textContent = name;
ballsCell.textContent = balls;
runsCell.textContent = runs;
let strikerate = balls > 0 ? (runs * 100) / balls : 0;
strikeRateCell.textContent = strikerate.toFixed(2);
}
document.querySelector('form').reset();
}
function selectRow(row) {
selectedRow = row;
document.getElementById('name').value = row.cells[0].textContent;
document.getElementById('balls').value = row.cells[1].textContent;
document.getElementById('runs').value = row.cells[2].textContent;
}
function removeSelectedRow() {
if (selectedRow) {
const table = document.getElementById('details');
table.deleteRow(selectedRow.rowIndex);
document.querySelector('form').reset();
selectedRow = null;
} else {
alert("Please select a row to remove.");
}
}
</script>
</body>
</html>