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 7 of 7
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts

    Question JQuery: name of a function as a callback instead of the actual function

    Can you give the name of a function as a callback instead of the actual function in JQuery

    ie

    Code:
    function doWhatever($e) {
         $e.preventDefault();
    
        if ($('#amount').val() == '' ) {
            alert("Please enter an amount");
            return;
        }
        if ($('#amount').val().match(/[^0-9\.]/g)) {
             alert("Your amount contains invalid characters");
            return;
        }
        if ($('#amount').val().match(/.*\..*\..*/)) {
            alert("Only 1 decimal point allowed");
            return;
        }
    /*    if ($('#amount').val().match(/^[0]+$/)|| $('#amount').val().match(/^([0]+)?.([0]+)?$/)  ) {
            alert("Your number cannot equal zero");
            return;
        }*/
    
    
        $('#value').attr("value", ($('#amount').val() * $('#cur2').val()/$('#cur1').val()).toFixed(2));
        }
    
    $(document).ready(function () {
        $('#submit').click(
    doWhatever($e)
    )
        
        
    
    });


  • #2
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Code:
    $(document).ready(function () {
        $("#submit").click(function(){
    	  doWhatever($e);
    	  });
    });
    Last edited by seco; 10-13-2009 at 04:54 AM.

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I see this a lot, where the function is assigned to a variable and the variable is then used as the callback:

    PHP Code:
    var whateverFunction = function ($e) {
         
    $e.preventDefault();

        if ($(
    '#amount').val() == '' ) {
            
    alert("Please enter an amount");
            return;
        }
        if ($(
    '#amount').val().match(/[^0-9.]/g)) {
             
    alert("Your amount contains invalid characters");
            return;
        }
        if ($(
    '#amount').val().match(/.*..*..*/)) {
            
    alert("Only 1 decimal point allowed");
            return;
        }

        $(
    '#value').attr("value", ($('#amount').val() * $('#cur2').val()/$('#cur1').val()).toFixed(2));
        }

    $(
    document).ready(function () {
        $(
    '#submit').click(whateverFunction)
    }); 

  • #4
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts
    I don't like hacks but this is the best there is

    thx


  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    you were almost there in your first post, just omit the parens so that you pass click the function instead of the result of executing the function...

    Code:
    $(document).ready(function () {
        $('#submit').click(doWhatever);
    });
    it's not a hack at all, in fact it's a good practice to name all your functions.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    nikos101 (10-13-2009)

  • #6
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    if you want to use more than one function on click use the one i posted.

  • #7
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts

    Question

    Quote Originally Posted by rnd me View Post
    you were almost there in your first post, just omit the parens so that you pass click the function instead of the result of executing the function...

    Code:
    $(document).ready(function () {
        $('#submit').click(doWhatever);
    });
    it's not a hack at all, in fact it's a good practice to name all your functions.
    ULTRA cool, its the same as FLEX



  •  

    Posting Permissions

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