JQuery function called in .bind handler works on the finished document

I defined a simple function and bound the click handler to the link using .bind ().

The function does not work on the click - rather, it works on the finished document. I don’t know why this is happening.

I am using jQuery 1.3.2.

HTML

<a href="#">click me</a>

JQuery

$(document).ready(function(){
  leftTurn = function($message){
    alert ($message);        
  };

  $('a').bind('click', leftTurn('hello'));
});

The code is also here in JSFiddle

+3
source share
2 answers

I think you can use the bind callback / handler:

$('a').bind('click', function(){
    leftTurn();    
});

Demo

From docs :

 .bind( eventType, [ eventData ], handler(eventObject) )
+3
source

You call the function when you turn it ()on at the end. The function .bind()expects a link to the function itself, and not to the called result.

leftTurn = function(){
        alert ('hello');        
    };

$(document).ready(function(){
    $('a').bind('click', leftTurn);
});
+5
source

Source: https://habr.com/ru/post/1780913/


All Articles