Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery: Select element but NOT child elements

Posted on 2011-09-27
3
Medium Priority
?
757 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
[X]
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
3 Comments
 
LVL 43

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 43

Accepted Solution

by:
David S. earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)
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…
Suggested Courses

618 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