1

I have three div's...

<div id="1"></div>
<div id="2"></div>
<div id="3"></div>

When I click on a random div, it moves the order of the div to the top (Let's use ID #2 for example). So the div's could be out of chronological order after this point. Example below...

<div id="2"></div>
<div id="1"></div>
<div id="3"></div>

So if this is the case, is there a way to get the last div ID I clicked?

Joe
  • 1,605
  • 7
  • 26
  • 34
  • It's either the ID of the first or second div, depending on what you mean by *last* and when you want that ID. – Felix Kling Feb 28 '12 at 17:40
  • 1
    Is it the one your clicking right now, or the last one you clicked before you clicked this one, if you get what I mean ? – adeneo Feb 28 '12 at 17:41
  • I need to get the id of the div that I previously clicked. Not the current and not the previous in div order – Joe Feb 28 '12 at 17:46
  • 1
    Several answers below are then correct, choose one that you believe solves your problem and accept it. Archer was the first with a correct answer I think. – adeneo Feb 28 '12 at 17:49
  • this would have been a super simple exercise with a better thought out question – charlietfl Feb 28 '12 at 17:53

7 Answers7

5

Do you mean this?

var lastID;

// document.ready
$(function() {
    $("div").on("click", function() {
        lastID = $(this).attr("id");
    });
});

function something() {
    // lastID is the id of the last div clicked.
}

Declaring lastID outside the document.ready function means it's globally accessible, so you can use it in other functions later.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
3

The last one you clicked will be at the top, so use the :first selector:

var lastId = $('div:first').attr('id');
lamplightdev
  • 2,041
  • 1
  • 20
  • 24
1
var lastClicked;

$("div").on("click", function() {
    console.log(lastClicked); //last one
    console.log(this.id); //this one
    lastClicked = this.id; //makes this the last one
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
0
var lastOne="";   
 $(function({   
        $('div').on("click",function(){
           console.log(lastOne+"div was clicked before");
            lastOne=$(this).attr('id');// id of the clicked div           
        });
    }));

maybe this helps you !

mas-designs
  • 7,498
  • 1
  • 31
  • 56
0
lastId = $("div#parent div:first").attr('id');
trapper
  • 11,716
  • 7
  • 38
  • 82
0

Make a var named LastId, make this var outside of the click function. and inside the click funciton update it with its id. so like

$(document).ready(function(){
    var LastId = "";

    $('div').on("click",function() {
        LastId = $(this).attr('id');
    });
});

Hope this helps :D

Dan Spiteri
  • 373
  • 1
  • 4
  • 20
  • you don't now if the div was added dynamically ! so you should use .on – mas-designs Feb 28 '12 at 17:44
  • Ahh :P Didnt know about that one, im mainly php than jquery, i dont use it as much – Dan Spiteri Feb 28 '12 at 17:47
  • @EvilP - if so every other answer is also wrong, as none of them use on() for delegation, but just the regular click equalent. Let the downvoting begin! – adeneo Feb 28 '12 at 17:47
  • @adeneo All other DO use .on so no downvoting will take place, downvoting shouldn't be considered a mean thing it should show that some people are wrong and there are better or correct ways. – mas-designs Feb 28 '12 at 17:49
  • @EvilP - I agree, but this answer is just as correct as the ones that use on(), click() is not deprecated, but on() is the preferred way of doing it, but for dynamically inserted elements, all the answers are wrong, as on() would have to have a third parameter for it to work with delegation. – adeneo Feb 28 '12 at 17:53
  • @adeneo: `on` is not preferred over `click`. `on` replaces `bind`, `live` and `delegate`, that's all. Or would you say that `bind` was preferred over `click`? – Felix Kling Feb 28 '12 at 18:04
  • @EvilP: As for dynamic elements: As far as I can see the OP does not mention anything like this, so why make this assumption? Do have to do this every time now someone asks a jQuery question? The logic is the same nevertheless. – Felix Kling Feb 28 '12 at 18:04
  • As I understand it, as of jQuery 1.7, `click()` is just an alias for `on()`, so they might do the same thing. – Code Junkie Feb 28 '12 at 18:08
  • @FelixKling - Well, it seems on() is preferred on SO :) The point was that click is just as correct, but it received a downvote, and the reason was probably that it does not work with dynamic elements, wich none of the other answers do either, and as you say, it was'nt part of the question to begin with, but a stupid reason for downvoting, that's all. And yes Junkie, click() uses on() inside, so to speak, but on() can be used with more parameters to also work with dynamic elements? – adeneo Feb 28 '12 at 18:12
  • So if you think I did the downvoting you are wrong ! .on() is simply the best way to ensure that this will work for now, and in the future. – mas-designs Feb 29 '12 at 08:30
0

I actually often use classes for this like:

HTML:

<div class="orderable">
  <div id="1"></div>
  <div id="2"></div>
  <div id="3"></div>
</div>

jQuery:

$(".orderable").on("click", "div", function() {
  $(".orderable .lastClicked").removeClass("lastClicked");
  $(this).addClass("lastClicked").prependTo(".orderable");
});

So that when things move around, I have the last element identified:

On div click:

<div class="orderable">
  <div id="2" class="lastClicked"></div>
  <div id="1"></div>
  <div id="3"></div>
</div>

Also as others have mentioned, in your example, the last clicked may always be the first in the list in which case you could go:

jQuery:

$(".orderable div:first") // Probable selector for last clicked
Code Junkie
  • 1,332
  • 3
  • 14
  • 24