[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread Charlie Griefer
On Fri, Apr 10, 2009 at 5:46 PM, thought thou...@orcon.net.nz wrote: Given this code: DIV id=div_one/DIV DIV id=div_two/DIV DIV id=div_three/DIV DIV id=div_four/DIV Lets say that the user is invited to click one one of these divs. How do I get the id of the div that was clicked ?

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread jay
$('div').click(function(){ alert(this.id); }); On Apr 10, 8:46 pm, thought thou...@orcon.net.nz wrote: Hi all. This might be more of a javascript problem rather than a jquery problem, but I'm also wondering if there is a 'jquery way' of solving it. Given this code: DIV id=div_one/DIV

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread Jordon Bedwell
script type=text/javascript $(document).ready(function() { $(div[id^='div_']).click(function() { alert(this.id); }); }); /script Better not to use JQuery for such trivial things as getting the ID attribute since it's already apart of the

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread thought
Thanks for the swift replies. I've got a lot to learn about javascript, and at this point, adapting charlies code, I get a strange effect that I didn't anticipate, and don't understand. $('.thumbnail').live(click, function(){ $(div[id^='thumbnail']).click(function() {

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread jay
A live handler is different from a normal handler, and I'm not sure why you're putting a normal handler inside of a live handler. A live handler works by looking at the target of whatever is clicked and comparing it to the selector, in this case, the thumbnail class. I personally prefer to use

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread thought
So then this behaviour is working because I'm using a live handler? In this case I am indeed using a live handler because elements of the thumbnail class don't exist when the document is initially created, but are conditionally added at a later point. I'm reasonably sure that this is an

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread James
I think the approach you want is just: $(div[id^='thumbnail']).live(function() { alert( $(this).attr('id') ); }); On Apr 10, 4:00 pm, thought thou...@orcon.net.nz wrote: So then this behaviour is working because I'm using a live handler? In this case I am

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread brian
The problem is that you've got a handler inside of a handler. You want either of these: $('.thumbnail').live(click, function(){ alert( $(this).attr('id') ); }); $('.thumbnail').click(function() { alert( $(this).attr('id') ); }); Whether you use $('.thumbnail') or

[jQuery] Re: How do get the ID of an element that has been clicked ?

2009-04-10 Thread thought
Thanks guys. It looks like Brians first fragment addresses the problem I have. On Apr 11, 2:04 pm, brian bally.z...@gmail.com wrote: The problem is that you've got a handler inside of a handler. You want either of these: $('.thumbnail').live(click, function(){         alert(