Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How do I add a click handler to a class and find out which element was clicked?

I have been using the following method for adding a click event to an id, I was wondering if I could do the same with a class.... I have a number of items (which are created in a for each loop) and I need to be able to click them and then pickup which was clicked... here is my existing code

$('submit-button').bind('click', submit_click);


function submit_click() {
    alert('I am clicked');
}

I was wondering if there is some way to pass in a variable into my function for the click so i can check the ID?? or similar

hence this

function submit_click(element) { // notice element
    alert(element + ' clicked');
}

Any help really appreciated

Thank you

EDIT

I have tried the following and in debug "elem" is undefined...

$('.clear').bind('click', clear_click($(this)));

   function clear_click(elem) 
   {
        alert(elem.attr("id"));
   }

WORKING SOLUTION

I have the working solution but I don't fully understand why, I would love to know why it works..

First of all I tried

 $('.clear').bind('click', clear_click($(this)) );

This seemed to work "BUT" when I loaded the page it enter the "clear_click" method without being clicked - strange...

Then I tried this..

 $('.clear').bind('click', function() { clear_click($(this)) } );

This works great! But I don't understand why I must pass a function and then within this function call my clear_click.

Can anyone explain why 1 works and the other doesn't?

Whenever I need to call a callback function or similar I should first open a function() and then call the method inside the function?

like image 413
mark smith Avatar asked Dec 16 '09 14:12

mark smith


People also ask

How do I know which element I clicked?

To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

How do you check if an element has been clicked jQuery?

Answer: Use the target Property You can simply use the event. target property to get the class from any element which is clicked on the document in jQuery. In this solution there is no need to know the element beforehand.

How do I get the clicked element in an event?

Getting the Element Clicked on from the Event ObjectaddEventListener('click', onClick); We call window. addEventListener to attach the click event listener to the html element. Then we get the element that's clicked on from the event.

What event handler is called when the user clicks on an element?

The <button> element has an event called 'click' that fires when the user clicks the button. Objects that can fire events have an addEventListener() method, that takes at least two arguments: the name of the event and a function to handle the event.


1 Answers

$(".yourclass").click ( function() {
    $(this).attr ( "id" ); //S(this) returns the current element
});

and you can code like this

$('.yourclass').bind('click', function() { submit_click($(this)) });

function submit_click(elem) 
{
    alert ( elem.attr ("id" ) );
}

Edit

   $('.clear').bind('click', function() { clear_click($(this)) });

   function clear_click(elem) 
   {
        alert(elem.attr("id"));
   }

This will work fine for you.

like image 94
rahul Avatar answered Oct 22 '22 14:10

rahul