最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I add a click handler to a class and find out which element was clicked? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Apr 28, 2022 at 20:27 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Dec 16, 2009 at 14:33 mark smithmark smith 20.9k47 gold badges136 silver badges190 bronze badges 1
  • Please see my edit to my answer. – rahul Commented Dec 17, 2009 at 4:04
Add a comment  | 

5 Answers 5

Reset to default 10
$(".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.

Update

To answer your second question:

You can bind a function as a second argument when using the click event, but you cant bind a function and apply arguments. On the other hand, there is no need to send this as an argument to the clear_click function since the this keyword inside the function refers to the element itself:

So this works in your case:

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

function clear_click()  {
  alert(this.id);
}

Sending this as an argument is not needed and bad coding:

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

In the event handler, the first argument is the event object. You can extract the clicked element from that object using currentTarget or target. In jQuery, this always refers to the currentTarget in the event handler context:

var handler = function(e) {
  var id = this.id; // this == e.currentTarget
}

$('submit').click(handler); // .click(fn) is shorthand for .bind('click', fn)

More examples:

$('submit').bind('click', function(e) {

    console.log(e.target) // the target that was clicked on
    console.log(e.currentTarget) // the element that triggered the click
    console.log(this) // the same as above

});

Just add $(this) to your function, You don't need to send any parameters because you are still in the context of the clicked element.

function submit_click() { // notice element
            alert($(this).attr('id') + ' clicked');
        }

When you bind a handler to a function, the clicked element will be the first argument

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

function submit_click(element){
  //element is the .submit-buttom element
  alert(element+'  was clicked');
  alert($(element)+' was clicked');
}

This should work:

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

function submit_click(element) { // notice element
   alert($(element).attr("id") + ' clicked');
}
发布评论

评论列表(0)

  1. 暂无评论