Leonardo Gandini
Leonardo Gandini

Reputation: 330

Assign numbers to unordered items

I'm a newbie with JQuery (and in general with code) but what I'm trying to do is this:

I've 5 div, I want that when a user click on a first one it show an alert with the number one, than with the second the number two etc. The point is that there's no a specific order of elements, I'm looking for a way to check if and how many other divs has or hasn't changed state and assign a progressive number to them!

For example (in my fiddle) if I click on the last one, I want the number one showed, than on another one, the number two etc...

something like:

$('div').each()

maybe?

I hope this is clear, this is a fiddle: http://jsfiddle.net/LeonardoGandini/CE6B3/16/

Upvotes: 0

Views: 43

Answers (1)

Chamika Sandamal
Chamika Sandamal

Reputation: 24302

Try this and it will give you what you want

$('div').click(function() {
    alert( $(this).index()+1 );
});

http://jsfiddle.net/CE6B3/19/


Update

Is this enough for your new chenge?

$('div').click(function() {
    $(this).addClass("clicked");
    alert( $(".clicked").length);
});

http://jsfiddle.net/CE6B3/22/

Upvotes: 2

Related Questions