Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2011, 01:53 PM   PM User | #1
sho88
New Coder

 
Join Date: Jul 2011
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
sho88 is an unknown quantity at this point
jQuery alert



Hi guys,

Here's my code...

Code:
$(document).ready(function() {
    $('#wrapper #headerLinks ul li a').hover(function() {         
        alert('hello!');
    });
});
I have a problem now, the alert repeats itself twice, where as I want it to alert once.
I tried adding "return" at the end of the code after 'alert("hello")', and it still showed the alert twice.

What can I do to make it alert once?
sho88 is offline   Reply With Quote
Old 11-27-2011, 03:07 PM   PM User | #2
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
The .hover() method can take two parameters of function type — the first one is intended for the mouseenter event handler and the second one — for the mouseleave event handler.
If you pass a single function as a parameter of the .hover() method, it will be executed for both mouseenter and mouseleave events.
So, try to change your code in the following way:
Code:
$(document).ready(function() {
    $('#headerLinks ul li a').hover(
        function() { alert('hello!'); },
        function() { /* do nothing */ }
    );
});
Alternatively, you can simply use the .mouseenter() method to bind you handler to the target elements.
__________________
I am still learning English

Last edited by Amphiluke; 11-27-2011 at 03:12 PM..
Amphiluke is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 PM.


Advertisement
Log in to turn off these ads.