jQuery: Select element but NOT child elements

Posted on 2011-09-27
Last Modified: 2012-05-12
Using jQuery, how can I trigger an event when a user hovers over an element but NOT over one of the element's child elements?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src=""></script>

<script type="text/javascript">
$(document).ready(function() {
 $('#selection').hover(function() { alert('You are hovering over the red area NOT the blue area.') }, function() {});

<style type="text/css">

#selection {
height: 500px;
width: 790px;
overflow: hidden;
position: relative;
padding: 0; 
margin: 0 auto;
background: red;

#selection p {
background: blue;
height: 500px;
overflow: hidden;
padding: 0; 
margin: 0 auto 0 auto;
width: 300px;



<div id="selection">


Open in new window

Question by:hankknight
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 42

Expert Comment

by:David S.
ID: 36711996
hover() is a shortcut for mouseenter() and mouseleave(), but you want the behavior of mouseover(), not mouseenter(). Check out the demo on this page:
LVL 16

Author Comment

ID: 36712089
Thanks but I think you missed the point of my question.  I want to know how to select an element without also selecting its children.  I used hover() as an example.  


Open in new window

LVL 42

Accepted Solution

David S. earned 500 total points
ID: 36712671
You can't do that with selectors. You have to trap the event on the descendants so it doesn't bubble up to the parent.

$(document).ready(function() {
  $('#selection').mouseover(function() { alert('You are hovering over the red area NOT the blue area.') });
  $('#selection').find('*').mouseover(function(e) { e.stopPropagation(); return false; });

Open in new window

P.S. Is there a particular reason why you're using that old version of jQuery? The current version is 1.6.4.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question