Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variables?

Status
Not open for further replies.

ejrhodes

MIS
Jul 9, 2001
20
US
I am experimenting with AJAX and I am running into an issue.


On my page, I have multiple spans with a similar naming convention. eg showtext1, showtext2, showtext3. On the click of another span, I pass my AJAX functions a paramater. One of these parameters is a number that corresponds to the number following showtext. I want to pass the resulting response text to the innerhtml of the correct span.


My current code is:


id=showtext1
id=showtext2, etc.

What I want to do is on the click of a span, I call a javascript function. I pass the function a parameter called temp3 that contains an integer (1, 2, 3, etc)

Code:

function showQuestion(url2, str, temp3, temp4)

{ xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

{
alert ("Browser does not support HTTP Request")
return
}

public var a=temp3;
var url=url2;
url=url+"?id="+str;
xmlHttp.onreadystatechange=stateChanged2;

xmlHttp.open("POST",url,true);

xmlHttp.send(null);

}

function stateChanged2()

{

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

{
b="showtext" + a;
document.getElementById(b).innerHTML=xmlHttp.responseText

}

}



I am trying to pass a public variable to my 2nd function stateChanged2 but I keep getting an error about a missing ;. If I cant set a public variable, how can I dynmically call the correct showtext span?
 
Remove the words "public var" from your variable declaration.

"Public" isn't a valid keyword in JS to define a varible (AFAIK), and using "var" will give it local scope.

That aside, why not pass it into your return function? That way, you'l be able to use this reuqest more than once without causing issues.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I tried passing it to my function but I kept getting a type mismatch error.

I originally tried:

function showQuestion(url2, str, temp3, temp4)

{ xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

{
alert ("Browser does not support HTTP Request")
return
}


var url=url2;
url=url+"?id="+str;
xmlHttp.onreadystatechange=stateChanged2(temp3);

xmlHttp.open("POST",url,true);

xmlHttp.send(null);

}

function stateChanged2(a)

{

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

{
b="showtext" + a;
document.getElementById(b).innerHTML=xmlHttp.responseText

}

}
 
So then how would I pass the variable to the function?
 
CLFlava, as always, you are the man. Thanks for your help, that fixed it.
 
Well you both are the man :) THis was driving me absolutely crazy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top