Open In App

Difference Btween $(this) and event.target

Last Updated : 11 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

$(this) and event.target Both are used to refer to the elements in the context of the event handling but serve different purposes and have different usages. This article will explore the differences between $(this) and event.target providing the descriptions, syntax and example code for each.

These are the following topics that we are going to discuss:

What is $(this)?

In jQuery, $(this) is used to the refer to the current HTML element within the context of a jQuery event handler. It wraps the JavaScript this keyword in a jQuery object providing the access to jQuery methods.

Syntax:

$(this)

Example: In this example, $(this) refers to the button that was clicked allowing to the manipulate it using the jQuery methods.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>$(this) Example</title>
    <script src=
"https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
    <button class="btn">Click Me</button>
    <script>
        $(document).ready(function () {
            $('.btn').on('click', function () {
                $(this).text('Clicked!');
                $(this).css('background-color', 'yellow');
            });
        });
    </script>
</body>

</html>

Output:

cl1

What is event.target?

In plain JavaScript, event.target refers to the element that triggered the event. It is a property of the event object and provides the reference to the DOM element that was the actual target of the event.

Syntax:

event.target

Example: In this example, event.target refers to the button that was clicked allowing to manipulate it directly using the plain JavaScript.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>event.target Example</title>
</head>

<body>
    <button class="btn">Click Me</button>
    <script>
        document.querySelector('.btn').addEventListener('click', function (event) {
            event.target.textContent = 'Clicked!';
            event.target.style.backgroundColor = 'yellow';
        });
    </script>
</body>

</html>

Output:

cl1

Difference Between $(this) and event.target

Characteristics

$(this)

event.target

Definition

Refers to the current HTML element within the context of the jQuery event handler wrapped in a jQuery object.

Refers to the element that triggered the event provided by the event object in the plain JavaScript.

Syntax

$(this)

event.target

Library

Requires jQuery

Native JavaScript

Usage Context

Used within the jQuery event handlers.

Used within the native JavaScript event listeners.

Example Code

$(this).css(‘background-color’, ‘yellow’);

event.target.style.backgroundColor = ‘yellow’;

Advantages

Provides access to jQuery methods for the easy manipulation.

Direct reference to the DOM element without the needing a library.

Performance

Slightly slower due to the jQuery overhead.

Faster as it uses native JavaScript.

Conclusion

Understanding the difference between $(this) and event.target is crucial for the effective event handling in JavaScript and jQuery. While $(this) is convenient for the jQuery users offering the easy access to the jQuery methods, event.target is the go-to choice in plain JavaScript providing the direct access to the DOM element that triggered the event. Choosing between them depends on whether we are using the jQuery or plain JavaScript and the specific needs of your project.



Similar Reads

jQuery event.target Property
The jQuery event.target is an inbuilt property that, is used to find which DOM element will start the event. Syntax: event.targetParameter: It does not accept any parameter because it is a property, not a function. Example 1: This example shows the working of event.target Property. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;styl
2 min read
HTML | DOM target Event Property
The target event property in HTML DOM is used to return the element that triggered the event. Syntax: event.target Return Value: This property returns the reference of the object on which the event originally occurred. Below example illustrates the target Event Property in HTML DOM: Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html
1 min read
Difference between Event Bubling & Event Capturing ?
Event BubblingEvent bubbling is the default behavior in which an event triggered on a nested element propagates up through its ancestors in the DOM hierarchy, allowing each ancestor to respond to the event. Event Capturing Event capturing is the opposite of event bubbling, where the event is captured on the outermost ancestor first, and then it pro
1 min read
Difference between "blank" and "_blank" target attributes in HTML
If you have ever noticed on the website that few links got opened either in a new tab or in a new window or sometimes, it might also happen that in some websites, click on the link, for the first time, it will open in a new tab &amp; that particular open tab will update &amp; reused every time whenever we click to the link. Both these scenarios can
3 min read
What is Event bubbling and Event Capturing in JavaScript ?
Event bubbling and event capturing are the two interesting concepts of JavaScript. Before diving deep into these fascinating concepts, let us first know about what an event listener is? An event listener is basically a function that waits for an event to occur. That event can be anything like a mouse click event, submitting a form, pressing keys of
6 min read
What is the use of the Event.preventDefault() method in Event Handling in JavaScript ?
The Event.preventDefault() method in JavaScript is used within event handlers to prevent the default behavior associated with an event from taking place. When an event occurs, such as a click on a link or the submission of a form, there is often a default action associated with that event. preventDefault() allows developers to stop this default beh
1 min read
How to target all Font Awesome icons and align them center?
Font Awesome is a great toolkit for developers to get icons based on CSS and LESS. There is other icons pack on the internet, but Font Awesome is more popular in the developer community. It has a wide collection of icons that are free to use. The most preferred way to center Font Awesome Icons is to assign a center class to each &lt;i&gt; tag. Sett
2 min read
How to specify the relationship between the current document and the target URL in HTML5 ?
We specify the relationship between the current document and the target URL by using a &lt;link&gt; tag in HTML. A link tag is used to define a link between a document and an external resource. It is used only when the href attribute is present. Approach: To specify the relationship between the current and the linked document use the rel attribute
2 min read
How to use the target attribute and the rel attribute in the &lt;a&gt; Tag ?
The target and rel attributes in the &lt;a&gt; (anchor) tag is used to control the behaviour of hyperlinks, specifying how the linked content should be opened and establishing relationships between the current and linked documents. Here's how to use these attributes: target Attribute:The target attribute determines where the linked content will be
2 min read
How to target desktop tablet and mobile using Media Query?
Media queries allow targeting different devices by adjusting CSS styles based on their screen size and resolution. By specifying breakpoints, developers can optimize layouts for desktop, tablet, and mobile devices, ensuring a responsive design across various screen sizes. The table below shows the screen resolutions of different devices: DeviceMax
2 min read
Article Tags :
three90RightbarBannerImg