This project will give you a basic understanding of bootstrap and how to target element using javascript
---------------
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<!-- Bootstrap CSS -->
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
crossorigin="anonymous"
/>
<title>College Library</title>
</head>
<body>
<!-- navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">College Library</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#"
>Home <span class="sr-only">(current)</span></a
>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input
class="form-control mr-sm-2"
type="search"
placeholder="Search"
aria-label="Search"
/>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">
Search
</button>
</form>
</div>
</nav>
<!-- navbar ends -->
<!-- alert starts -->
<div id="msg">
</div>
<div class="container my-4">
<h2>College Library</h2>
<hr />
<br />
<form id="libraryForm">
<div class="form-group row">
<label for="bookName" class="col-sm-2 col-form-label"
>Book Name</label
>
<div class="col-sm-10">
<input
type="text"
class="form-control"
id="bookName"
placeholder="Enter Book Name"
/>
</div>
</div>
<div class="form-group row">
<label for="author" class="col-sm-2 col-form-label">Author</label>
<div class="col-sm-10">
<input
type="text"
class="form-control"
id="author"
placeholder="Enter Book Author Name"
/>
</div>
</div>
<fieldset class="form-group">
<div class="row">
<legend class="col-form-label col-sm-2 pt-0">Book Type</legend>
<div class="col-sm-10">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="type"
id="fiction"
value="fiction"
checked
/>
<label class="form-check-label" for="fiction">
Fiction
</label>
</div>
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="type"
id="programming"
value="programming"
/>
<label class="form-check-label" for="programming">
Computer Programming
</label>
</div>
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="type"
id="cooking"
value="cooking"
/>
<label class="form-check-label" for="cooking">
Cooking
</label>
</div>
</div>
</div>
</fieldset>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Add Book</button>
</div>
</div>
</form>
<!-- table starts -->
<div id="table">
<h1>Your Books</h1>
<br />
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Number</th>
<th scope="col">Name</th>
<th scope="col">Author</th>
<th scope="col">Type</th>
</tr>
</thead>
<tbody id="tableBody">
<!-- Dynmically from Java script -->
</tbody>
</table>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<!-- <script src="index.js"></script> -->
<script src="practise.js"></script>
<script
src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"
></script>
</body>
</html>
index.js:
-----------
console.log("hello jee");
var index = 0;
class Book {
constructor(name, author, type) {
this.name = name;
this.author = author;
this.type = type;
}
// method to display prototype i.e to dipslay the content
add(book, index) {
// console.log(`book = ${book} and index = ${index} `);
let tableBody = document.getElementById("tableBody");
let uiString = `
<tr>
<th scope='row'>${index}</th>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.type}</td>
</tr> `;
tableBody.innerHTML = tableBody.innerHTML + uiString;
return tableBody.innerHTML;
}
// method to remove the entred data from UI
clear() {
let libraryForm = document.getElementById("libraryForm");
libraryForm.reset();
return libraryForm;
}
// #book an authr name entered by user should have 2 or more letters
validate(book) {
console.log("inside validate", book);
if (book.name.length < 2 || book.author.length < 2) return false;
else return true;
}
// #shwing the validation succes/failure
show(msgType, displayMsg) {
let msg = document.getElementById("msg");
msg.innerHTML = `<div class="alert alert-${msgType} alert-dismissible fade show" role="alert">
<strong> ${displayMsg} </strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
setTimeout(function () {
msg.innerHTML = "";
}, 2000);
}
}
// add submit event listner to library form
let libraryForm = document.getElementById("libraryForm");
libraryForm.addEventListener("submit", libraryFormSubmit);
function libraryFormSubmit(e) {
let name = document.getElementById("bookName").value;
let author = document.getElementById("author").value;
let fiction = document.getElementById("fiction");
let programming = document.getElementById("programming");
let cooking = document.getElementById("cooking");
let type;
if (fiction.checked) {
type = fiction.value;
} else if (programming.checked) {
type = programming.value;
} else {
type = cooking.value;
}
// creating object of book class
book = new Book(name, author, type);
console.log("form submitted", book);
if (book.validate(book)) {
index = index + 1;
book.add(book, index);
book.clear();
book.show("success", "Successfully Added");
} else {
book.show("danger", "Please try again!!");
}
e.preventDefault();
}
0 Comments