[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

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

0
robrodp
Asked:
robrodp
  • 6
  • 5
1 Solution
 
GaryCommented:
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

0
 
Shaun KlineLead Software EngineerCommented:
You can use the plus symbol to concatenate strings and variables together:
xmlhttp.open("GET","update.asp?arma=" + tmpScr, true);
0
 
robrodpAuthor Commented:
Now how do I concatenate that into my line 24
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GaryCommented:
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;
0
 
robrodpAuthor Commented:
I dont't get your last comment . I have no var tmpStr = "";
0
 
GaryCommented:
function changeFunc() {
    var select1 = document.getElementById("armadora");
var tmpStr = "";


Just put before that function

var tmpStr;
0
 
robrodpAuthor Commented:
I saw it... removed
0
 
robrodpAuthor Commented:
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);
0
 
GaryCommented:
Post your code as you have it.
0
 
robrodpAuthor Commented:
Sorry I had a Typo

Getting there

Thx Gary
0
 
GaryCommented:
That code you are using is very old unless you really need to support IE6 and lower you can just replace your loadXMLDoc() function with this

function loadXMLDoc()
{
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("GET","update.asp?arma=" . tmpScr. ",true);
    xmlhttp.send();
    //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}

Open in new window

0
 
robrodpAuthor Commented:
Thx Gary...

That sounds excellent

Roberto
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now