Last Click Jquery

How can I determine the last clicked id ?! Because if I use:

 if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }

It will return the current click on the button. I have something like this:

$('#menu).click(function(event) {

  clicks++;
   if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }
   console.log(lc);

And I have two buttons. Now, if I click on the first button of the console, the log will show undefinded, but if I click on the second button, it will show its identifier. I want to prevent this.

+3
source share
2 answers

You can use .data()for this,

<ul class="menu">
   <li id="menu1">1</li>
   <li id="menu2">2</li>
   <li id="menu3">3</li>
   <li id="menu4">4</li>
   <li id="menu5">5</li>
</ul>

$(".menu li").click(function() {
    if($(this).parent().data("lastClicked")){
        alert($(this).parent().data("lastClicked"));
    }
    $(this).parent().data("lastClicked", this.id);
});​

http://jsfiddle.net/aDUdq/

+4
source

The variable lcis local. Instead, you should write:

var lc;
if(clicks > 1){
 lc = event.target.id;  // Last Clicked event
 }
console.log(lc);
+1
source

All Articles