html: Accessing a mouse event from onclick attribute

Alkis Mavridis

I know that I can access a mouse event if I assign the mouse listener with js:

myElement.addEventListener("click", e => console.log(e.pageX))

My question is: is it possible to access the mouse event when assigning the listener by html attribute?

<div onclick="alert('where is the mouse event?')"></div>
CertainPerformance

The onclick will be evaluated as the inside of a function, and that function will have an argument of the event, so you can do it like this:

<div onclick="console.log(arguments[0].pageX)">text</div>

But you really, really shouldn't. Always attach listeners with Javascript instead of HTML-inline-eval. Code will be easier to write, easier to read, safer, and more maintainable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing data from mouse event in Svelte

From Javascript

Getting data-* attribute for onclick event for an html element

From Dev

ReactJs - accessing state in the onclick event

From Dev

Set css attribute in onclick event

From Dev

Get tag attribute in Onclick event

From Dev

How to prepend html into on page element from HTML onClick event?

From Dev

html onclick event not firing

From Dev

Accessing event trigger button onclick function

From Dev

Onclick for each button add/remove class from attribute to HTML document

From Dev

Mouse onclick event target (Moving sidenavifation)

From Java

Scenebuilder/JavaFX polygons mouse event onClick

From Dev

Accessing a property from its attribute

From Dev

Calling a typescript method from your html button onclick event

From Dev

SetInterval does not run from HTML form's onClick event

From Dev

How do I use elements from html in javascript for an onclick event?

From Dev

Jquery - get from html attribute a callback name in event delegation

From Javascript

Access event to call preventdefault from custom function originating from onclick attribute of tag

From Dev

HTML onclick attribute with variable URL

From Dev

HTML onClick attribute not calling function

From Javascript

How to stop event propagation with inline onclick attribute?

From Dev

Call onclick attribute programatically and talk to the event parameter

From Dev

Why is calc not defined at onclick event attribute?

From Dev

Click event works correctly in onclick attribute but not as addeventlistener

From Dev

Python Beautiful Soup modify onclick Event Attribute

From Dev

Accessing key with space from JSON in HTML attribute (Angular 10 Property binding)

From Dev

Accessing Directive scope with in HTML element event handler

From Dev

JS onclick event from loop

From Dev

Accessing custom Lucene attribute from DirectoryReader

From Dev

Trouble accessing attribute from Stateful Widget

Related Related

  1. 1

    Accessing data from mouse event in Svelte

  2. 2

    Getting data-* attribute for onclick event for an html element

  3. 3

    ReactJs - accessing state in the onclick event

  4. 4

    Set css attribute in onclick event

  5. 5

    Get tag attribute in Onclick event

  6. 6

    How to prepend html into on page element from HTML onClick event?

  7. 7

    html onclick event not firing

  8. 8

    Accessing event trigger button onclick function

  9. 9

    Onclick for each button add/remove class from attribute to HTML document

  10. 10

    Mouse onclick event target (Moving sidenavifation)

  11. 11

    Scenebuilder/JavaFX polygons mouse event onClick

  12. 12

    Accessing a property from its attribute

  13. 13

    Calling a typescript method from your html button onclick event

  14. 14

    SetInterval does not run from HTML form's onClick event

  15. 15

    How do I use elements from html in javascript for an onclick event?

  16. 16

    Jquery - get from html attribute a callback name in event delegation

  17. 17

    Access event to call preventdefault from custom function originating from onclick attribute of tag

  18. 18

    HTML onclick attribute with variable URL

  19. 19

    HTML onClick attribute not calling function

  20. 20

    How to stop event propagation with inline onclick attribute?

  21. 21

    Call onclick attribute programatically and talk to the event parameter

  22. 22

    Why is calc not defined at onclick event attribute?

  23. 23

    Click event works correctly in onclick attribute but not as addeventlistener

  24. 24

    Python Beautiful Soup modify onclick Event Attribute

  25. 25

    Accessing key with space from JSON in HTML attribute (Angular 10 Property binding)

  26. 26

    Accessing Directive scope with in HTML element event handler

  27. 27

    JS onclick event from loop

  28. 28

    Accessing custom Lucene attribute from DirectoryReader

  29. 29

    Trouble accessing attribute from Stateful Widget

HotTag

Archive