0

I have written a Javascript Function

jQuery(document).ready( function newbie($) {

    //var email = 'emailaddress'
    var data = {
        action: 'test_response',
        post_var: email
    };
    // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
    $.post(the_ajax_script.ajaxurl, data, function(response) {
        alert(response);
    });
    return false;
});

Which I will call using

newbie();

But I want to pass in a variable (the email address) when I call the function but I am not sure how to do this. That $ sign seems to get in my way! Any thoughts much appreciated.

1
  • That's a named function expression, and its name is only known to itself Commented Jun 17, 2017 at 23:58

3 Answers 3

1
jQuery(document).ready(function(){
   var email = 'emailaddress';
   newbie(email);
});


function newbie(email) {
    var data = {
        action: 'test_response',
        post_var: email
    };
    // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
    $.post(the_ajax_script.ajaxurl, data, function(response) {
        alert(response);
    });
    return false;
}

OR

 jQuery(document).ready(function(){
        var newbie = function(email) {
           var data = {
               action: 'test_response',
               post_var: email
           };
           // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
           $.post(the_ajax_script.ajaxurl, data, function(response) {
               alert(response);
           });
           return false;
      }

     var email = 'emailaddress';
     newbie(email);
  });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Mohamed. If I use the first example (putting the first bit of code in my .js functions file) and the second bit within my page - nothing happens. Any thoughtso n what I'm doing wrong?
@LinzDarlington keep eyes on console for errors ..what error you got??
@LinzDarlington the arrange of files .*on first example*. 1- include jquery 2- run the function on document ready 3- include functions js file and you can also include functions js file before run the function 3 then 2 instead of 2 then 3.. BUT on my second example 1- include jquery 2- check you define the var newbie before you run the function .. with this arrange every thing should works fine
Thanks Mohamed. Will check and revert.
0

Functions in javascript take 'arguments'. You can pass in as many arguments you want and define their name space in the function declaration. ie

function foo(bar,baz,etc){
   console.log(bar,baz,etc);
}
foo(1,2,3)
 //logs out 1 2 3

Sometimes you don't always know what's going to be passed in or how many arguments there are going to be, in this case inside of the function declaration we can use the 'arguments' object to pick out certain arguments passed into the function.

function foo(){
   console.log(arguments);
}
foo(1,2,3)
 //logs out an array object that looks like this [1,2,3]

Comments

0

jQuery(document).ready( function newbie($, email) {

//var email = 'emailaddress'
var data = {
    action: 'test_response',
    post_var: email
};
// the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
$.post(the_ajax_script.ajaxurl, data, function(response) {
    alert(response);
});
return false;

});

you simply call the function by passing the values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.