Solved

jQuery: Select element but NOT child elements

Posted on 2011-09-27
3
722 Views
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

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

<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;
}

</style>

</head>
<body>

<div id="selection">
 <p>
 </p>
</div>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
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: http://api.jquery.com/mouseover/#example-0
0
 
LVL 16

Author Comment

by:hankknight
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.  

$('#selection<BUT_NO_CHILDREN').hover(.....

Open in new window

0
 
LVL 42

Accepted Solution

by:
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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 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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now