<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 object.  And 'this' is already an object in
modern browers.

 

From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Charlie Griefer
Sent: Friday, April 10, 2009 7:52 PM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: How do get the ID of an element that has been clicked
?

 

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 ?

 

<script type="text/javascript">

     $(document).ready(function() {

          $("div[id^='div_']").click(function() {  // when a div whose ID
attribute starts with "div_" is clicked...

               alert($(this).attr('id'));

          });

     });

</script>


-- 
I have failed as much as I have succeeded. But I love my life. I love my
wife. And I wish you my kind of success.

Reply via email to