April 26, 2012

need help to figure out what is the error, it's jQuery code

Question by user1743638

i’m trying to make something like light box for a message with jQuery and CSS
but it doesn’t work!!!
here is jQuery code

$(document).ready(function(e) {

    function myFunction(){
        $("#overLay").hide();
        $("#x").hide();
        $("#message").hide();
    }

    $("#overLay").click( myFunction() );
    $("#x").click( myFunction() );

});

Answer by muffel

function myFunction(){
    $("#overLay").hide();
    $("#x").hide();
    $("#message").hide();
}
$(function(){ //run after page is loaded
   $("#overLay").click(myFunction);
   $("#x").click(myFunction);
});

Answer by Starx

As for the syntactical part, The way you are calling the functions are wrong, call them without the parenthesis

$("#overLay").click( myFunction );
$("#x").click( myFunction );

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!