addEventListener to an element, with the element itself as parameter

alexluz

First of all, sorry if the title isn't the best one, I couldn't find a better one.

How can I set a function as clickEvent for a Div using javascript, and at the same time also set the parameter of the function, to be the Div itself?
For example:

I have a HTML file that has:

<span id='parent'>
    <div class='child'></div>
</span>

And a JS file where I have the following code:

var el = document.getElementById("parent").getElementsByClassName("child")[0];
el.onclick = someFuntion(this);

But the thing is, I want that the this paramenter refer to the div.child, so that when I click the Div it should pass itself as a parameter, as a DOM element, making me able to use it inside of someFuntion(element) just like any other DOM element: element#id, for example.

Solutions tried:

el.addEventListener("click", someFuntion(event), false);
el.addEventListener("click", someFuntion(this), false);
el.addEventListener("click", someFuntion(el), false);
$("#parent #child").bind("click", someFuntion(this));
el.onclick = someFuntion(divIntoSpan);

SOLUTION:
el.onclick = someFuntion;

function someFunction(e){
    if(e.target){
        //remember to use "e.target" from here on, instead of only "e"
        //e.id (error: undefined); e.target.id (correct)
    }
}
Jan Turoň

Solution

Just do el.addEventListener("click", someFunction); <- not someFunction()

and then use event.target

function someFunction(e) {
   if(e.target) ...
}

Alternative solution

If you like @JaromandaX's solution, you could simplify it to

el.addEventListener("click",someFunction.bind(null,el));

Note that bind returns function. This example should clarify it:

function someFunction(el) {
  // if used with bind below, the results are:
  console.log(this); // "This is it!"
  console.log(el); // the clicked element
}

a.addEventListener("click",someFunction.bind("This is it!",a));

Advice

  • someFunction is the function itself
  • someFunction() is what the function returns

If you want to be a good coder, you shouldn't be satisfied with It works!. It is worthy to spend some time to find out why it works. Without this step you can't be sure if your code doesn't contain hidden bugs in some cases.

What you have in Solutions tried is Programming by permutation which is an antipattern - I highly recommend reading this great wiki article about antipatterns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

addEventListener to all but one element

From Dev

addEventListener to all but one element

From Dev

addEventListener to an article element in Javascript

From Dev

Get the innerHTML of an element, but not the element itself

From Dev

Passing id of element but getting the element itself

From Dev

Changing the size of an element relative to itself

From Dev

JQuery trigger element itself in a bind

From Dev

Changing the size of an element relative to itself

From Dev

CSS: transition properties in element itself?

From Dev

addEventListener is not working when defined into a new element

From Dev

Find surrounding element of source iframe in addEventListener

From Dev

Firefox element.event works but element.addEventlistener does not

From Dev

Append an element with onclick parameter

From Dev

Pass parameter as an array element

From Dev

Call a method with element as parameter

From Dev

Can one element in struct access another element of itself in C?

From Dev

Cost of passing IEnumerable with only one element vs passing the element itself

From Dev

Match String version of Object element to Object element itself

From Dev

how to target the sibling of the element that has a same class as the element itself

From Dev

CSS: Styling an input element's text (as opposed to the input element itself)?

From Dev

Is it possible to compare a element with each element in array except itself in c++?

From Java

Hash code of ArrayList that contains itself as element

From Dev

How to validate if an element of an array is an array itself?

From Dev

JQuery element refering to itself inside chain

From Dev

How to create selection from DOM element itself

From Dev

Tap highlight on parent element of link, not link itself?

From Dev

Multiply each element in a vector by itself to create a matrix

From Dev

A deleting button deletes itself instead of the targeted element

From Dev

Hash code of ArrayList that contains itself as element

Related Related

  1. 1

    addEventListener to all but one element

  2. 2

    addEventListener to all but one element

  3. 3

    addEventListener to an article element in Javascript

  4. 4

    Get the innerHTML of an element, but not the element itself

  5. 5

    Passing id of element but getting the element itself

  6. 6

    Changing the size of an element relative to itself

  7. 7

    JQuery trigger element itself in a bind

  8. 8

    Changing the size of an element relative to itself

  9. 9

    CSS: transition properties in element itself?

  10. 10

    addEventListener is not working when defined into a new element

  11. 11

    Find surrounding element of source iframe in addEventListener

  12. 12

    Firefox element.event works but element.addEventlistener does not

  13. 13

    Append an element with onclick parameter

  14. 14

    Pass parameter as an array element

  15. 15

    Call a method with element as parameter

  16. 16

    Can one element in struct access another element of itself in C?

  17. 17

    Cost of passing IEnumerable with only one element vs passing the element itself

  18. 18

    Match String version of Object element to Object element itself

  19. 19

    how to target the sibling of the element that has a same class as the element itself

  20. 20

    CSS: Styling an input element's text (as opposed to the input element itself)?

  21. 21

    Is it possible to compare a element with each element in array except itself in c++?

  22. 22

    Hash code of ArrayList that contains itself as element

  23. 23

    How to validate if an element of an array is an array itself?

  24. 24

    JQuery element refering to itself inside chain

  25. 25

    How to create selection from DOM element itself

  26. 26

    Tap highlight on parent element of link, not link itself?

  27. 27

    Multiply each element in a vector by itself to create a matrix

  28. 28

    A deleting button deletes itself instead of the targeted element

  29. 29

    Hash code of ArrayList that contains itself as element

HotTag

Archive