Vinoth Kannan
Vinoth Kannan

Reputation: 99

get value from one function to another function using javascript

i want to get value from one function to the another function. and i want to pass the value to handler page. but in that i cant get the value to pass .. here i had given the code. please help me. for example assume that for radio=MR & fname=john.

function getdata()
{
   alert('hi');
   var radio = document.getElementsByName("radiobuttonlist1");
   for (var i = 0; i < radio.length; i++)
   {
    if (radio[i].checked)
    alert(radio[i].value);
   }

   var fname=document.getElementById("Firstnametxt").value;
   alert(fname);
}

here i want to get all those values to another function this is my another function.

function sendinfo()
{
getdata();

 $(document).ready(function(){     

   var url="Handler.ashx?radio="+radio+"&fname="+fname+""; "here i want the values from above function"

  alert(url);
    $.getJSON(url,function(json)
    {
        $.each(json,function(i,weed)
        {



        });

    });

});

}

thank you . help me

Upvotes: 3

Views: 47175

Answers (3)

Kevin Beal
Kevin Beal

Reputation: 10849

You can do this two ways.

Option 1

You can define those variables outside of both functions like:

var radio;
var fname;
getdata() {
    radio = document.getElementsByName("radiobuttonlist1");
    fname=document.getElementById("Firstnametxt").value;
    // VALUE UPDATED HERE
}
sendinfo() {
    // VARIABLE ACCESSIBLE HERE
}

And then whenever the values of those variables is updated it will be updated at the scope those variables were initially defined (outside those functions).

The scope of a javascript variable is within the curly braces {} that variable is in and any curly braces within that set. Scope in javascript often refers to those curly braces {}. There are exceptions including the if statement in which you can define variables inside of and access outside (assuming the condition was true).

Option 2

Or you can pass those variables as parameters to the second function like: sendinfo(radio, fname);

Passing values by returning them

You can return the values as an object literal in your getdata() function like this:

getdata() {
    return {
        radio : document.getElementsByName("radiobuttonlist1"),
        fname : document.getElementById("Firstnametxt").value
    }
}

Then do this:

sendinfo() {
    var data = getdata();
}

And then access those variables like: data.radio and data.fname.

Upvotes: 9

Hkachhia
Hkachhia

Reputation: 4539

var radio;
var fname;

function getdata()
{
   alert('hi');
   radio = document.getElementsByName("radiobuttonlist1");
   for (var i = 0; i < radio.length; i++)
   {
    if (radio[i].checked)
    alert(radio[i].value);
   }

   fname=document.getElementById("Firstnametxt").value;
   alert(fname);
}

function sendinfo()
{
    getdata();

     $(document).ready(function(){     

       var url="Handler.ashx?radio="+radio+"&fname="+fname+""; "here i want the values from above function"

      alert(url);
        $.getJSON(url,function(json)
        {
            $.each(json,function(i,weed)
            {



            });
        });
    });
}

Upvotes: 2

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2904

You can declare two variables globally,set them to the values you need in the first function and access them in the second function.

Upvotes: 2

Related Questions