jquery $(this).id return Undefined
Categories:
Resolving 'undefined' when Accessing ID with $(this).id
in jQuery

Understand why $(this).id
returns undefined
in jQuery and learn the correct methods to retrieve the ID attribute of an element in both jQuery and plain JavaScript contexts.
When working with jQuery, a common pitfall for developers, especially those transitioning from plain JavaScript, is attempting to access an element's id
attribute directly using $(this).id
. This often results in undefined
, leading to confusion. This article will clarify why this happens and demonstrate the correct, idiomatic ways to retrieve an element's ID using jQuery and standard JavaScript.
Understanding the jQuery Wrapper
The core of the issue lies in understanding what $(this)
represents in a jQuery context. When you wrap a DOM element (like this
inside an event handler) with $
(e.g., $(this)
), you are creating a jQuery object. This jQuery object is a wrapper around the native DOM element(s), providing a rich set of methods and properties specific to jQuery. It is not the raw DOM element itself.
flowchart TD A[Event Triggered] --> B{`this` keyword} B --> C["Native DOM Element (e.g., `<div id='myDiv'>`)"] C --> D["Accessing `this.id` (Correct for DOM)"] C --> E["Wrapping with jQuery `$(this)`"] E --> F["jQuery Object (Wrapper)"] F --> G["Accessing `$(this).id` (Incorrect - Returns `undefined`)"] F --> H["Accessing `$(this).attr('id')` (Correct for jQuery)"] F --> I["Accessing `$(this)[0].id` (Correct - Unwraps to DOM)"]
Flowchart illustrating the difference between native DOM element and jQuery object access.
Native DOM elements have properties like id
, className
, innerHTML
, etc., directly accessible. However, a jQuery object does not expose these native DOM properties directly on itself. Instead, jQuery provides its own methods to interact with these attributes and properties.
Correct Ways to Get an Element's ID
There are several correct ways to retrieve the ID of an element, depending on whether you prefer jQuery's API or direct DOM access.
$(this).attr('id')
is often the most idiomatic and readable approach. If performance is critical and you only need the ID, this.id
(without jQuery wrapping) is slightly faster.jQuery Method: .attr()
$('button').on('click', function() {
var elementId = $(this).attr('id');
console.log('jQuery .attr() method: ' + elementId);
// Example: elementId will be 'myButton'
});
jQuery Method: .prop()
$('button').on('click', function() {
var elementId = $(this).prop('id');
console.log('jQuery .prop() method: ' + elementId);
// Example: elementId will be 'myButton'
});
Native DOM Access
$('button').on('click', function() {
// 'this' refers to the native DOM element
var elementId = this.id;
console.log('Native DOM access: ' + elementId);
// Example: elementId will be 'myButton'
});
Unwrapping jQuery Object
$('button').on('click', function() {
// $(this)[0] unwraps the jQuery object to the native DOM element
var elementId = $(this)[0].id;
console.log('Unwrapping jQuery object: ' + elementId);
// Example: elementId will be 'myButton'
});
Why attr()
vs prop()
for ID?
While both attr('id')
and prop('id')
will correctly return the ID of an element, there's a subtle but important distinction between jQuery's .attr()
and .prop()
methods.
.attr()
(attribute): This method deals with HTML attributes as they are defined in the HTML source code. For example,id="myButton"
is an attribute..prop()
(property): This method deals with DOM properties, which are JavaScript properties of the DOM object. These properties can change dynamically and reflect the current state of the element.
For the id
attribute, both methods typically yield the same result because the id
attribute directly maps to the id
DOM property and is generally static. However, for other attributes like checked
, selected
, or value
, using .prop()
is often preferred as it reflects the current state of the element, whereas .attr()
might return the initial HTML attribute value.
<!DOCTYPE html>
<html>
<head>
<title>jQuery ID Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<button id="myButton">Click Me</button>
<input type="checkbox" id="myCheckbox" checked>
<script>
$(document).ready(function() {
$('#myButton').on('click', function() {
console.log('Button ID (attr):', $(this).attr('id'));
console.log('Button ID (prop):', $(this).prop('id'));
});
$('#myCheckbox').on('click', function() {
console.log('Checkbox ID (attr):', $(this).attr('id'));
console.log('Checkbox ID (prop):', $(this).prop('id'));
console.log('Checkbox Checked (attr):', $(this).attr('checked')); // Returns 'checked' or undefined
console.log('Checkbox Checked (prop):', $(this).prop('checked')); // Returns true or false
});
});
</script>
</body>
</html>
Full HTML example demonstrating attr()
vs prop()
for ID and other attributes.
ctl00_ContentPlaceHolder1_myButton
). If you need to get the client-side ID of a server control, ensure you're using the correct selector or ClientID
property on the server-side to generate the correct ID for client-side use.