Question
I have HTML structured like this, and I want to use a jQuery selector to select the child img inside the div when the div is clicked.
<div id="example">
<img src="image.jpg" alt="Example image">
</div>
Inside the click handler, I can refer to the clicked div using:
$(this)
How can I use jQuery to select the child img element from $(this)?
Short Answer
By the end of this page, you will understand how to select child elements from $(this) in jQuery, when to use .children() vs .find(), and how this pattern is used in click handlers and real projects.
Concept
In jQuery, $(this) usually refers to the current DOM element inside an event handler, such as the element that was clicked.
If you want to access elements inside that current element, jQuery gives you traversal methods such as:
.children()for direct children only.find()for any matching descendants inside the element
For the given HTML, the img is a direct child of the div, so either of these works:
$(this).children('img')
or
$(this).find('img')
This matters because real interfaces often react to user actions on a container and then update something inside it, such as:
- changing an image
- toggling a class on a nested element
- reading text inside a clicked card
- finding form fields inside a section
Understanding DOM traversal helps you write code that is scoped to the current element instead of searching the whole page.
Mental Model
Think of $(this) as the current folder you are standing in.
.children('img')means: look only at the files directly inside this folder.find('img')means: search anywhere inside this folder, including subfolders
So if you click one specific div, $(this) is that div, and you can search inside it without affecting other parts of the page.
Syntax and Examples
Core syntax
$(this).children('img')
$(this).find('img')
Example: direct child image
<div class="card">
<img src="photo.jpg" alt="Photo">
</div>
$('.card').on('click', function () {
const image = $(this).children('img');
console.log(image.attr('src'));
});
Explanation
$('.card').on('click', ...)attaches a click handler to each.card
Step by Step Execution
Consider this example:
<div class="box">
<img src="a.jpg" alt="A">
</div>
$('.box').on('click', function () {
const img = $(this).children('img');
console.log(img.attr('src'));
});
Here is what happens step by step:
- jQuery selects all elements with class
.box. - A click handler is attached to each one.
- When a user clicks a
.box, the function runs. - Inside the function,
thisrefers to the clicked.boxDOM element. $(this)wraps that DOM element as a jQuery object..children('img')looks at the direct children of that and keeps only elements.
Real World Use Cases
This pattern is very common in jQuery-based interfaces.
Clickable image cards
When a user clicks a card, you may want to:
- highlight the image inside it
- swap the image source
- open a larger preview
Product grids
A product tile may contain an image, title, and price. Clicking one tile should only affect that tile's contents.
$('.product').on('click', function () {
$(this).find('img').addClass('active');
});
Forms and validation
You might select inputs inside the currently submitted section rather than searching the whole page.
Accordions and panels
When a panel is clicked, developers often search inside it for icons, labels, or nested content to update.
Tables and rows
Clicking a row can trigger updates to buttons, badges, or links inside that specific row.
Real Codebase Usage
In real projects, developers rarely use broad selectors when the action should be limited to one component. Instead, they scope the search to $(this) or another known container.
Common pattern: scoped traversal
$('.gallery-item').on('click', function () {
const $img = $(this).find('img');
$img.toggleClass('highlighted');
});
This avoids accidentally selecting every image on the page.
Guard clause pattern
Sometimes the child may not exist.
$('.card').on('click', function () {
const $img = $(this).children('img');
if ($img.length === 0) {
return;
}
$img.addClass('loaded');
});
Working with data from child elements
Common Mistakes
1. Using a global selector instead of searching inside this
Broken:
$('img').addClass('active');
This selects all images on the page.
Better:
$(this).children('img').addClass('active');
2. Using .children() when the element is nested deeper
Broken for nested HTML:
$(this).children('img')
If the image is inside another wrapper, this returns nothing.
Better:
$(this).find('img')
3. Forgetting to wrap this in $()
Broken:
Comparisons
| Method | What it selects | Use when | Example |
|---|---|---|---|
.children('img') | Direct child img elements only | The img is immediately inside the current element | $(this).children('img') |
.find('img') | Any descendant img elements | The img may be nested deeper | $(this).find('img') |
.closest('div') | The nearest matching ancestor | You want to move upward in the DOM | $(this).closest('.card') |
Cheat Sheet
// Direct child only
$(this).children('img')
// Any matching descendant
$(this).find('img')
// Alternative scoped search
$('img', this)
// First matching child image
$(this).children('img').first()
// Read an attribute
$(this).children('img').attr('src')
// Add a class
$(this).children('img').addClass('active')
Quick rules
thisinside a normal jQuery event handler is the clicked DOM element- Wrap it with
$()to use jQuery methods - Use
.children()for direct children - Use
.find()for nested descendants - Avoid arrow functions if you rely on
this
FAQ
How do I get a child img from $(this) in jQuery?
Use:
$(this).children('img')
If the image may be nested deeper, use:
$(this).find('img')
What is the difference between .children() and .find() in jQuery?
.children() only looks at direct children. .find() searches all matching descendants inside the element.
Why does this.children('img') not work?
Because this is a plain DOM element in the event handler. jQuery methods require a jQuery object, so use $(this) first.
Can I use $('img', this) instead?
Yes. It is an alternative scoped selector that searches for img inside .
Mini Project
Description
Build a small clickable gallery where each tile contains an image. When a tile is clicked, only the image inside that tile should be highlighted. This demonstrates how to use $(this) with child selection in a practical UI pattern.
Goal
Create a click handler that selects and updates the image inside the clicked container only.
Requirements
- Create at least three clickable containers with images inside them.
- When a container is clicked, add a highlight class only to its own image.
- Remove the highlight class from images in other containers.
- Use
$(this)with a child-selection method. - Keep the solution in jQuery.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.