Xtal
Xtal

Reputation: 305

Jquery last click

How i can 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 button clicked id 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 2 buttons Now if i click on first button console log will show undefinded but if i click on second button it will show his id.I want to prevent that

Upvotes: 4

Views: 10046

Answers (2)

Chamika Sandamal
Chamika Sandamal

Reputation: 24302

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/

Upvotes: 5

Alexandre Khoury
Alexandre Khoury

Reputation: 4032

The variable lc is local. You must write instead :

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

Upvotes: 1

Related Questions