Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2009, 02:13 PM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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)
)
    
    

});
__________________

nikos101 is offline   Reply With Quote
Old 10-12-2009, 02:55 PM   PM User | #2
seco
Regular Coder

 
seco's Avatar
 
Join Date: Nov 2008
Location: Oregon
Posts: 682
Thanks: 5
Thanked 79 Times in 77 Posts
seco has a little shameless behaviour in the past
Code:
$(document).ready(function () {
    $("#submit").click(function(){
	  doWhatever($e);
	  });
});

Last edited by seco; 10-13-2009 at 04:54 AM..
seco is offline   Reply With Quote
Old 10-12-2009, 05:19 PM   PM User | #3
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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)
}); 
__________________
Fumigator is offline   Reply With Quote
Old 10-12-2009, 05:20 PM   PM User | #4
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
I don't like hacks but this is the best there is

thx
__________________

nikos101 is offline   Reply With Quote
Old 10-13-2009, 03:42 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
nikos101 (10-13-2009)
Old 10-13-2009, 04:53 AM   PM User | #6
seco
Regular Coder

 
seco's Avatar
 
Join Date: Nov 2008
Location: Oregon
Posts: 682
Thanks: 5
Thanked 79 Times in 77 Posts
seco has a little shameless behaviour in the past
if you want to use more than one function on click use the one i posted.
seco is offline   Reply With Quote
Old 10-13-2009, 09:43 AM   PM User | #7
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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
__________________

nikos101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:14 AM.


Advertisement
Log in to turn off these ads.