tags:

views:

290

answers:

3

When one of the divs inside 'uploadChoice' is clicked, how can I ascertain which one is clicked? Can I return an 'eq' value somehow?

 <div id=uploadChoice>
     <div>Text</div>
     <div>Image</div<
    <div>Text & Image</div>
</div>



$("#uploadChoice div").click(function(){
    //insert code here! :)   
});
+4  A: 
$('#uploadChoice div').click(function(event) {
  var index = $('#uploadChoice div').index(this);
});
brianng
top marks my friend! :)
Stephen
A: 

You can get the contents of the div with

$("#uploadChoice div").click(function(){
    var choice = $(this).html;
}

And then just do a switch statement or some nested ifs.

Personally though, I'd add an ID to each of the divs and just check the ID of the clicked element.

idrumgood
Can I ask why this was voted down? It certainly takes care of what the original poster was asking for. In fact, it provides two options for identifying which option a user clicked on.Also, this solution would allow the user to re-order their buttons without having to change any code, as it uses the value (or ID) of the buttons rather than their relative position.
idrumgood
Ah, I think I understand. The OP was looking specifically for a way to do this using 'eq', so my solutions didn't satisfy that. Nevermind.
idrumgood
+2  A: 

An easier alternative that does not require duplicating a selector is the following

$('#uploadChoice div').click(function(event) {
  var index = $(this).prevAll().length;
});
redsquare
Nice! Sadly i'll leave brianng the marks as he got to me quicker :) Thanks for the help!
Stephen
The marks is the best solution. Not fastest:) Imagine you came here via google. Which answer would you want to see!
redsquare
Definately brianng's now you've said that ;) :)
Stephen
yu[, thats a great help for the community...
redsquare