1

I have a small issue: I need to pass the value of string from function A to function B which will take it as argument and use it.

I have tried the following but its not working.

   // first function (A)
   $("a#SayHello").click(function (e) {
       e.preventDefault();

       var x = function () {
               var dt = '{"ProductID": "' + $("input#ProductID").val() + '" , "AffiliationURL": "' + $("input#AffiliationURL").val() + '" , "Quantitiy": "' + $("input#Quantitiy").val() + '" , "PricePerUnit": "' + $("input#PricePerUnit").val() + '" , "commissionAmount": "' + $("input#commissionAmount").val() + '"}';
               return dt.toString();
           };
       alert(x);
       $.B(x);
   });

   // second function
   function B(dt) {

       $.ajax({
           type: 'POST',
           data: dt,
           url: 'http://localhost:4528/WebSite1/WebService.asmx/InsertCommissionRecord',
           contentType: 'application/json; charset=utf-8',
           dataType: 'json',
           success: function (data, textStatus, XMLHttpRequest) {
               alert(data.d);
               alert(XMLHttpRequest);

           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
               alert(textStatus);
           }
       });
   };
Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
SoftBuilders Pk
  • 109
  • 4
  • 15

3 Answers3

4

I'm not sure but your not executing the function, try this:

alert(x());
$.B(x());
RTB
  • 5,773
  • 7
  • 33
  • 50
0

Instead of

$.B(x);

call x() and B() straight away:

B(x());


To be able to use B() like this: $.B() you need to add your function to jQuery's $:
$.B = function(){...

or

jQuery.B = function(){...

But it's generally not recommended to pollute $ - it's much better to use your own namespace.

See here: is-it-possible-to-create-a-namespace-in-jquery

Community
  • 1
  • 1
jfrej
  • 4,548
  • 29
  • 36
  • I just did the following and it worked for me `$("a#SayHello").click(function (e) { e.preventDefault(); var x = A(); // alert(x); B(x); }); // first function (A) function A() { var dt = '{"ProductID": "' + $("input#ProductID").val() + '" , "AffiliationURL": "' + $("input#AffiliationURL").val() + '" , "Quantitiy": "' + $("input#Quantitiy").val() + '","commissionAmount": "' + $("input#commissionAmount").val() + '"}'; return dt.toString(); };` – SoftBuilders Pk Jul 18 '12 at 12:26
0

Try call the B function without " $. ", just B() , like any simple function.

Also, you can add an alert(dt) inside B() function, to check the data from parameter.

CoursesWeb
  • 4,179
  • 3
  • 21
  • 27