Answer to Question #309250 in HTML/JavaScript Web Application for king

Question #309250

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

HTML

<!DOCTYPE html>

<html>

<head></head>

<body>

  <div class="display-username text-center">

    <p>Hello, <span id="name">Varun</span>!</p>

    <p>Update your Name</p>

    <input id="inputValue" placeholder="Write your name here" />

    <br />

    <button class="btn-primary save-button" id="saveBtn">

      Save to Local Storage

    </button>

    <button class="btn-secondary reset-button" id="resetBtn">Reset</button>

  </div>

</body>


</html>



1
Expert's answer
2022-03-10T18:07:27-0500
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <div class="display-username text-center">
        <p>Hello, <span id="name">Varun</span>!</p>
        <p>Update your Name</p>
        <input id="inputValue" placeholder="Write your name here" />
        <br />
        <button class="btn-primary save-button" id="saveBtn">Save to Local Storage</button>
        <button class="btn-secondary reset-button" id="resetBtn">Reset</button>
    </div>


    <script>
        let name = document.getElementById('name');
        let inputValue = document.getElementById('inputValue');
        let saveBtn = document.getElementById('saveBtn');
        let resetBtn = document.getElementById('resetBtn');


        saveBtn.onclick = () => {
            localStorage.setItem('name', inputValue.value);
            name.innerText = inputValue.value;
        };


        resetBtn.onclick = () => {
            localStorage.setItem('name', 'Varun');
            name.innerText = 'Varun';
        };


        document.addEventListener("DOMContentLoaded", () => {
            name.innerText = localStorage.getItem('name');
        });
    </script>
</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS