tags:

views:

20

answers:

3

i have this piece of html

<li><a href="#" id="9000" class="yes vpslink"><img src="x.gif" /></a></li>
<li><a href="#" id="9001" class="no vpslink"><img src="x.gif" /></a></li>


$('.vpslink').click(function(e) {
   var id='i dont know dude'; 
   alert('you clicked on id'+id);
}); 

How do I find the id of this class, link ?

+2  A: 

Inside the click handler:

alert($(this).attr('id'));
carpie
thanks, and how do i then change this content; like replace the x.gif by y.gif ? and background color of the 'a' element ?
Disco
Well, since $(this) is the link element, then $(this).next() will be the img element. At that point you can set any attribute, such as 'src' with attr() (e.g. $('this').next().attr('src', 'y.gif');) You can set css styles, such as background, with .css('background', '#aaa')
carpie
A: 
   $('.vpslink').click(function(e) {
   var id= $(this).attr('id'); 
   alert('you clicked on id'+id);
}); 
Shota Bakuradze
+1  A: 
$('.vpslink').click(function(e) {

      // Quick way to get the ID
   var id = this.id;

      // Replace the SRC of the sibling <img> 
   $(this).siblings('img').attr('src', function(i,src) {
        return (src == 'x.gif') ? 'y.gif' : 'x.gif';
   });

});

The fastest way to get the ID of the element is to access its DOM property directly with this.id.

You can use .siblings() to get the sibling <img> and .attr() to update the source. The .attr() method can take a function as a parameter that returns the value to set.

patrick dw