jQuery Event How to - Handle click event once








Question

We would like to know how to handle click event once.

Answer


<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--   w  w w.  j ava  2s .com-->
                $("div").one("click", function(){
                  console.log("clicked.");
                });
        });
    </script>
    <style>
      div { width:50px; height:70px; float:left; margin:5px;
            background:rgb(255,140,0); cursor:pointer; }
      
    </style>
  </head>
  <body>
       <div>asdf</div>
  </body>
</html>

The code above is rendered as follows: