views:

26

answers:

2

i wonder why the browser displays "logged in" without me having clicked on the link i id-tagged with clickhere. it displays it everytime i refreshes the page.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(login());

 });
</script>

i just want it to be displayed when i click on the link.

what is the problem?

+4  A: 

You have to remove () after login in JQuerys click() function.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(login);

 });
</script>
ammoQ
that was really weird. usually you call a function like login()..hmm..
never_had_a_name
exactly. But you don't want to call it (right now), you just want to tell jquery to use this function object later, when the click happens.
ammoQ
A: 

You have to pass the method to jQuery's click(). login() mean execute-the-login-method. You have to pass method correctly as ammoQ said.

Or you can change your code like this. It will only call login() when click the #clickhere.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(function() {
        login();
    });

 });
</script>
Ei Maung