Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function from another jQuery

Tags:

jquery

    $(document).keydown(function(e){
        if (e.keyCode == 37) { 
            return false;
        }
    });

    $(".direction").click(function() {
        var direction = $(this).text();

When I click on a button with .direction class the second function above is called. When the left key is pressed I want to call the $(".direction").click(function() { But with a value (Instead of the var direction = $(this).text(); part) It would be var direction = value passed to function;

How can I do that?

like image 476
John Avatar asked Sep 05 '26 14:09

John


1 Answers

Add another function that is used by both methods:

$(document).keydown(function(e){
     if (e.keyCode == 37) { 
          move("left");
     }
});

$(".direction").click(function() {
     move($(this).text());
});

function move(newDirection)
{
     var direction = newDirection;
}
like image 177
bjornruysen Avatar answered Sep 07 '26 08:09

bjornruysen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!