3

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

1
  • 1
    But you need to call the function first where the value will be assigned. Then use it for the second function. If you call the function before assigning , you'll not get your expected value Commented Nov 8, 2012 at 5:04

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

ok but i had done that what u said. its showing the alert as like thisHandler.ashx?radio=undefined&fname=undefined
Ah, thank you! Not a big user of JavaScript, but I was just looking to do this. Personally, I feel option 1 is just clearer for my little head... but I have to ask. Is this still considered a good practice in 2021? Cheers.
@deijmaster It depends on who you ask. Personally, I prefer the 2nd solution for most cases as the values can't be used out of order, and are garbage collected sooner.
2

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

Comments

2
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)
            {



            });
        });
    });
}

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.