People who can help me, I’m a beginner in javascript,

Asked

Viewed 54 times

-2

I wanted to have the amount of clicks stored in the Torage locale, and when I returned the number of clicks continued where it left off. I tried to do so, but no alert only shows NaN:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        var p = document.createElement("p");
        document.body.appendChild(p);

    $(document).ready(function(){

         var cout = localStorage.getItem('cout') || 0;

            $("#btnCount").click(function(){
                cout = parseInt(cout)+1;
                $('p').html(cout);
                alert(cout);
                localStorage.setItem('cout',cout);
            });         
    });    
    </script>
    <button type="button" id="btnCount">Click me</button>
</body>
</html>

1 answer

0

   <script type="text/javascript">
        var p = document.createElement("p");
        p.setAttribute('id','num');
        document.body.appendChild(p);

    $(document).ready(function(){


        cout = localStorage.getItem('cout');
        cout = parseInt(cout) || 0; 

            $("#btnCount").click(function(){
                cout = cout+1;
                $('p').html(cout);
                // alert(cout);
                localStorage.setItem('cout',cout);
            });         
    }); 
  • 1

    This is an answer to your question or more information that should belong to the question ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.