Skip to content

JavaScript target

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 target

The Event.target property returns which DOM element triggers activity.

Syntax:

event.target


target Examples

Example 1

target 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.target.nodeName)
        }

    </script>
</body>

Example 2

target 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>
    <p id="txt"></p>

    <script>

        function myFunction(event) {
            let node = event.target;
            document.getElementById("txt").innerHTML = node.tagName;
        }

    </script>
</body>

Example 3

target example
<body>

    <button onclick="myFunction()">Button</button>
    <div id="box"></div>

    <script>

        function myFunction() {
            event.target.addEventListener("click", message)
        }
        function message() {
            alert("truecodes")
        }

    </script>
</body>

Example 4

target example
<body>

    <div class="box"></div>

    <script>

        let box = document.querySelector(".box");

        box.addEventListener("click", (e) => {
            e.target.classList.toggle("active");
        });

    </script>
</body>

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: