Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: jQuery alert

  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    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.
    Last edited by Amphiluke; 11-27-2011 at 03:12 PM.
    I am still learning English


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •