Reputation: 5246
This is getting really frustrating now!
I've created a script that appends some content from another page to the body when you hover a special link. I also set it up so that whenever i move the mouse away from the link, the new element (with the loaded content) hides. So far, so good. The problem is that sometimes when i move the cursor away from the link too fast, the script doesn't react (or something). Ah well, i thought, and just added this evil code that refuses to work:
$('#userBubble').mouseover(function() {
$(this).hide();
});
Now, is there anything wrong with that? I've tried using mouseenter instead, but no luck with that either. I've also tried putting it inside a $(document).ready() function - no luck there either. #userBubble is the the container of the content loaded on hover, and the name is correct. I can console.log #userBubble, so it does in fact exist.
Any ideas what's messing this up? I get no errors, it just doesn't work.
Upvotes: 1
Views: 177
Reputation: 38290
The following should work.
$(document).ready( function()
{
$("#hoverHide").mouseover( function ()
{
$(this).hide();
} );
} );
If that does not work, try this:
$(document).ready( function()
{
$("#hoverHide").mouseover( function ()
{
$("#hoverHide").hide();
} );
} );
If neither of those works, the it seems likely that you have a syntax error somewhere on your page. Perhaps in the .ready
function declaration.
Upvotes: 0
Reputation: 11615
Make sure that user ID is unique in the DOM and that the element exists when you bind(or use live
).
Change it to a class and use live. I bet it works.
<img id="userBubble" class="userBubble"/>
$('.userBubble').live("mouseover", function() {
$(this).hide();
});
Upvotes: 2
Reputation: 1464
Have you tried this?
$('#userBubble').mouseover(function(event) {
$(event.target).hide();
});
EDIT: Ohw, didn't quite read your question well.
Like Spiny Norman said, you have to use 'live' or 'delegate' ; )
Upvotes: 0
Reputation: 15172
It worked with hover
$('#userBubble').hover(function() {
$(this).hide();
});
Upvotes: 0
Reputation: 3963
make sure you have loaded the html-div before you set the event:
<!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>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#test').mouseover(function () { $(this).hide(); })
});
</script>
</head>
<body>
<div id="test">This will hide!</div>
</body>
</html>
Upvotes: 0
Reputation: 8327
This is probably because the element does not yet exist when you add the handler. Try
$('#userBubble').live('mouseover', function() {
$(this).hide();
});
instead.
Upvotes: 2