Skip to content
Advertisement

javascript mouse events

Is there a way in javascript to make it so that an “onclick” works for all members of a particular class? So if I have objects A and B that are both of type X, clicking on either of them will call the same function? But that function should only work on whichever of A or B was called, not both at the same time.

Because basically what I’m trying to do is when I click on either A or B they move to a different location, but I want this to be able to work for any arbitrary number of n elements in the same class

Advertisement

Answer

DEMO : http://jsfiddle.net/2BRS9/

Add the same class for all those html elements . You can get the element from which the event was fi

If the classname is “myclass”

  $(".myclass").live({
      click: function(e){
      // You can get id by many ways, some shown here :
      id= this.id;
      console.log(id);
      id = $(this).attr("id");
      console.log(id);
      id = e.target.id;  
      console.log(id);

   }

});​

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement