Link to home
Start Free TrialLog in
Avatar of robrodp
robrodpFlag for Mexico

asked on

passing and using a variable from one js function to another

I have this code:

The changeFunc grabs a variable and assigns it to tmpScr

The it calls the function loadXMLDoc()

In the line that reads:

xmlhttp.open("GET","update.asp?arma=" . tmpScr. ",true);

I want tmScr to be like:
xmlhttp.open("GET","update.asp?arma=tmpScr",true); (Whatever value it has into line 24)

I have 2 questions:

1. How do I pass the variable (tmpScr)

2. How do I insert it in the I need



function changeFunc() {
    var select1 = document.getElementById("armadora");
var tmpStr = "";
for (var i = 0; i < select1.length; i++) {
        if (select1.options[i].selected)
            tmpStr = select1.options[i].value;
            
    }
    //alert(tmpStr);
    loadXMLDoc();
   }
	
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","update.asp?arma=" . tmpScr. ",true);
xmlhttp.send();
//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}

Open in new window

Avatar of Gary
Gary
Flag of Ireland image

Declare the variable outside any function then it becomes a global variable that you can use in any function

var myVar;
function someFunction1(){}
function someFunction2(){}

Open in new window

You can use the plus symbol to concatenate strings and variables together:
xmlhttp.open("GET","update.asp?arma=" + tmpScr, true);
Avatar of robrodp

ASKER

Now how do I concatenate that into my line 24
See the comment above by Shaun, which I undeleted.

Remove this var tmpStr = ""; from inside the function once you have added this before the function
var tmpStr;
Avatar of robrodp

ASKER

I dont't get your last comment . I have no var tmpStr = "";
function changeFunc() {
    var select1 = document.getElementById("armadora");
var tmpStr = "";


Just put before that function

var tmpStr;
Avatar of robrodp

ASKER

I saw it... removed
Avatar of robrodp

ASKER

For some reasonit is not passing the variable.

If I hard code it it does its job.
xmlhttp.open("GET","update.asp?arma=something", true);

if tmpScr=something
If I leave it concatenated to doesn´t (xmlhttp.open("GET","update.asp?arma=" + tmpScr, true);
Post your code as you have it.
Avatar of robrodp

ASKER

Sorry I had a Typo

Getting there

Thx Gary
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robrodp

ASKER

Thx Gary...

That sounds excellent

Roberto