Skip to content
Advertisements

DOM Event currentTarget

The Event interface represents any event that occurs in the DOM; Some are generated by the user (such as mouse or keyboard events), others are produced by APIs (such as events that show whether an animation is working, videos are paused). There are many types of events, some of which use other interfaces based on the main Event interface. The event itself includes features and methods common to all events.

  • event.type: Returns the name of the event type when it is triggered.
  • event.currentTarget: The event listeners return the item that triggered the event.
  • event.target: Returns the element that triggered the event.
  • button: The read-only property indicates which mouse button is pressed to trigger the event.
  • The buttons: read-only property indicates which buttons are pressed on the mouse (or other input device) when the event is triggered.
  • event.timeStamp: Indicates the time that indicates when the event occurred.
  • event.clientX: Returns the horizontal coordinate of the mouse pointer over the current window when the mouse event is triggered.
  • event.clientY: Returns the vertical coordinate of the mouse pointer relative to the current window when the mouse event is triggered.
  • event.pageX:Returns the horizontal coordinate of the mouse pointer relative to the document when the mouse event is triggered.
  • event.pageY: Returns the vertical coordinate of the mouse pointer relative to the document when the mouse event is triggered
  • altKey: Returns whether the “ALT” key is pressed when a key event is triggered.
  • ctrlKey: Returns whether the “CTRL” key is pressed when a key event is triggered.
  • shiftKey: Returns whether the “SHIFT” key is pressed when a key event is triggered.

DOM Events currentTarget

The CurrentTarget event property returns an event listener that triggers the event when a user clicks a nested item.
This is especially useful during capturing and bubbling.
The CurrentTarget method always refers to the element to which the event handler was added, unlike event.target, which identifies the element where the event occurred.

Syntax:

event.currentTarget


currentTarget Examples

Example 1

currentTarget example
<body onclick="myFunction(event)">

    <button>Button</button>
    <p>Paragraph</p>
    <ul>
        <li>List</li>
    </ul>
    <div>Div</div>
    <nav>Nav</nav>
    <code>code</code>

    <script>

        function myFunction(event) {
            alert(event.currentTarget.nodeName)
        }

    </script>
</body>

Example 2

current Target Example
<body>

    <p>truecodes.org</p>

    <script>

        document.body.addEventListener('click', color, false);

        function color(e) {
            e.currentTarget.style.color = 'red';
            console.log(e.currentTarget);
        }

    </script>
</body>
Advertisements

6 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: