4

I am trying to get the class or an id of the last clicked element. This is what I have based off of what I found here...

HTML

<a href="" class="button">Button</a>

JQUERY

$('.button').click(function () {
          myFuntion();
});

function myFunction (e) {
 e = e || event;
 $.lastClicked = e.target || e.srcElement;

 var lastClickedElement = $.lastClicked;
 console.log(lastClickedElement);

}

This sort of does what I want, but I am not sure how to go about modifying it so I can get just the class.

I have also tried using this solution but couldn't get it to work with my code.

$('.button').click(function () {
  myFuntion();
});


function myFunction(){
    var lastID;
    lastID = $(this).attr("id");

    console.log(lastID);
}

When I do this my console log comes back as undefined. I am probably missing something obvious. Any help is much appreciated. Thanks.

Community
  • 1
  • 1
Kris Hollenbeck
  • 15,752
  • 18
  • 64
  • 99
  • Thanks everybody for the fast responses. I will look through these starting with the first reply and see if I can get anything to work. – Kris Hollenbeck Aug 29 '12 at 16:00

5 Answers5

1

A couple of ways come to mind:

$(".button").click(myFunction);

Should work with the above myFunction.


$(".button").click(function () { myFunction($(this)); });
function myFunction($elem) {
   var lastID;
   lastID = $elem.attr('id');
   $.data('lastID', lastID);
}
Explosion Pills
  • 183,406
  • 48
  • 308
  • 385
1

You can pass clicked element as parameter to your function:

$('.button').click(function () {
    myFunction(this);
});

function myFunction(element) {
    console.log(element);
    console.log(element.id);
    console.log($(element).attr("class"));
}

UPDATE added jsfiddle

Zbigniew
  • 26,284
  • 6
  • 55
  • 64
1

In order to get the class-name of the element, assuming you have an accurate reference to the element from which you want to retrieve the data:

var lastClickedElement = $.lastClicked,
    lastClickedElementClassNames = lastClickedElement.className;

This does return the full list of all the classes of the element though.

David Thomas
  • 240,457
  • 50
  • 366
  • 401
  • I tried this with what I have and I get the whole HTML element in my console.log .. Here is my fiddle. http://jsfiddle.net/krishollenbeck/hZh9B/5/ – Kris Hollenbeck Aug 29 '12 at 16:25
1
$('.button').click(function () {
  myFuntion(this);
});


function myFunction(ele){
    var lastID;
    lastID = $(ele).attr("id");

    console.log(lastID);
}
Dan Barzilay
  • 4,766
  • 3
  • 26
  • 39
0

First Select all possible DOM Elements

 var lastSelectedElement = null

    $(document).ready(function(){
       $("*").live("click",function(){
       lastSelectedElement = $(this);
       myFunction($(this));
      });
    });

   function myFunction(element) {
console.log(element);
console.log(element.id);
console.log($(element).attr("class"));
 }

than you could play with lastSelectedElement by grabbing it's ID or Class with jQuery .attr("ID OR CLASS");

Rosmarine Popcorn
  • 10,515
  • 11
  • 56
  • 88