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
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

820 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