Remove Item in Local Storage
The goal of this coding exam is to quickly get you off the ground with
Remove Item In Localstorge
Use the below reference image.
https://assets.ccbp.in/frontend/content/dynamic-webapps/removeItem-in-localStorage-op.gif
Achieve the given functionality using JS
<!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>
</head>
<body>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
padding: 15px;
position: relative;
width: 400px;
height: 200px;
display: flex;
align-items: center;
justify-content: space-around;
flex-direction: column;
}
input {
height: 35px;
padding: 10px;
}
.btn {
outline: none;
cursor: pointer;
background: none;
padding: 5px;
color: white;
}
.save {background: rgb(91, 134, 255);}
.reset {background: gray;}
</style>
<div class="wrapper">
<p>Hello, <span class="name">Stranger</span>!</p>
<p>Update your Name</p>
<input type="text" placeholder="Write your name here">
<div class="buttons">
<button class="btn save" onclick="save()">Save to Local Storage</button>
<button class="btn reset" onclick="reset()">Reset</button>
</div>
</div>
<script>
const name = document.querySelector('.name'),
input = document.querySelector('input')
function reset() {
localStorage.setItem('name', '');
name.textContent = 'Stranger';
}
function save() {
if (input.value) {
input.style.background = 'white'
localStorage.setItem('name', input.value)
name.textContent = input.value
input.value = ''
} else {
input.style.background = '#ff7b64'
}
}
if (localStorage.getItem('name'))
name.textContent = localStorage.getItem('name')
</script>
</body>
</html>
Comments
Leave a comment