tags:

views:

93

answers:

2

How i can make a function click on image that have a classname 'img'

<img src='img' class="img" id="fu1">
<img src='img' class="img" id="fu2">
<img src='img' class="img" id="fu3">

i want to make a function on click of image who have a class 'img'

how can i track the id of img who is click

ex: if i click first image then how i can track 1 .

+5  A: 

For the completely changed question: You can use the class they have for a selector and just reference this.id, like this:

$('.img').click(function() {
  alert('hi, my ID is: ' + this.id);
});

Original:

You can use their IDs in a selector, like this

$("#rem1, #rem2, #rem3").click(function() {
    alert('hi, my source is:' + this.src);
});

Or, give them a class, like this:

<img id="rem1" src="img" class="myClass" />
<img id="rem2" src="img" class="myClass" />   
<img id="rem3" src="img" class="myClass" />

and use that class for your selector, like this:

$(".myClass").click(function() {
  alert('hi!');
});

In either case, be sure to wrap this code in a document.ready event handler so it doesn't run until the elements are ready, like this:

$(function() {
  $(".myClass").click(function() {
    alert('hi, my source is:' + this.src);
  });
});
Nick Craver
@4thpage ... Adding a class to this is very much the way to go, much more efficient.
Justin Jenkins
A: 

Try this:

$("img[src='img']").click(function() {
    alert('You clicked');
});

More info here: http://api.jquery.com/attribute-equals-selector/

BFOT