-1

I have 5 elements with a class foo. and a function is attached with them like this

$('.foo').on('click',someFunction)

But for one of function is off(). I want to loop through these elements and find that element for which function is off. Can anyone tell me how to achieve this.

Alaeddine
  • 1,571
  • 2
  • 22
  • 46
Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42
  • What do you mean by "But for one of them function is off()"? Do you want to know which element doesn't have `someFunction` click handler? – Ram Sep 17 '15 at 12:10
  • possible duplicate of [jQuery to loop through elements with the same class](http://stackoverflow.com/questions/4735342/jquery-to-loop-through-elements-with-the-same-class) – Francesco de Guytenaere Sep 17 '15 at 12:12
  • @Vohuman when I click one element with foo class, function runs `someFunction` and makes `$(this).off('click',someFunction)`. Now I want to get this clicked element. – Nafees Anwar Sep 17 '15 at 12:14
  • `this` refers to that element. Done! – Ram Sep 17 '15 at 12:15

2 Answers2

1

You say that you unbind the handler the element is clicked, so this keyword already refers to that element! You can store the clicked elements in array or a jQuery collection using jQuery.prototype.add method and use them whenever you need.

Another option is an adding an identifier (e.g. a class name) to the element:

$(this).off('click', someFunction).addClass('bar');

Now you can simply get the target elements by using:

$('.foo.bar');
Ram
  • 143,282
  • 16
  • 168
  • 197
0

If you want to refer to this element in another function or somewhere else in the script, just use a global variable (either a string for the last or an array for all "click-ignoring" element/s) and store it in there maybe like this:

var lastClickedElement = "";
$(".foowrap .foo").each(function(){
    $(this).on("click", function(){
        $(this).off("click");
        lastClickedElement = $(this);
    });
});

or, if you don't want to store the whole element, you can just store it's index. to do so, replace lastClickedElement = $(this); with lastClickedElement = $(this).index();. You could then just refer to it as $(".foowrap .foo").index(lastClickedElement).