Get the element triggering an onclick event in jquery?

后端 未结 4 608
暗喜
暗喜 2020-12-08 09:15

I have a form where i\'ve replaced the submit button with an input (with type=button) with an onclick which calls an existing function:

4条回答
  •  感动是毒
    2020-12-08 09:48

    It's top google stackoverflow question, but all answers are not jQuery related!

    $(".someclass").click(
        function(event)
        {
            console.log(event, this);
        }
    );
    

    'event' contains 2 important values:

    event.currentTarget - element to which event is triggered ('.someclass' element)

    event.target - element clicked (in case when inside '.someclass' [div] are other elements and you clicked on of them)

    this - is set to triggered element ('.someclass'), but it's JavaScript element, not jQuery element, so if you want to use some jQuery function on it, you must first change it to jQuery element: $(this)

    When your refresh the page and reload the scripts again; this method not work. You have to use jquery "unbind" method.

提交回复
热议问题