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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing value to getElementById in function

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I have a form with a text box. Next to the text box is a span that I've called Title2 and it starts with a ? in. The idea is when you start typing it changes from a ? to 'OK'.

Eventually there will be lots of of these so I want to have a function that handles it. I am calling the function as follows:

Code:
onKeyUp="checkData(this.value, '1', 'Title2')"

And my checkData function looks like this:

Code:
var xmlhttp
function checkData(DataTyped, ValidationType, NameOfSpan)

{
if (DataTyped.length==0)
  {
  document.getElementById(NameOfSpan).innerHTML="";
  return;
  }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="validate.asp";
url=url+"?DataTyped="+DataTyped+"&ValidationType="+ValidationType+"&NameOfSpan="+NameOfSpan;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById(NameOfSpan).innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

However I am getting an error that NameOfSpan is undefined. However if I output some data using my validate.asp script it is definitely getting the NameOfSpan value.

Any ideas?

Thanks very much

Ed
 
Hi All,

After a bit more research I found using a global variable was the way to do this:

Code:
var xmlhttp;

//divName = the div on the page your calling all this where the results will be displayed.

function checkData(DataTyped, ValidationType, NameOfSpan){
gElem = NameOfSpan; //Global variable
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null){
alert ("Browser does not support HTTP Request");
return;
}
var url="validate.asp";
url=url+"?DataTyped="+DataTyped+"&ValidationType="+ValidationType+"&NameOfSpan="+NameOfSpan;
xmlhttp.onreadystatechange=stateChanged; 
xmlhttp.open("GET",url,true);
xmlhttp.send(null); 
}

function stateChanged(){ 
if (xmlhttp.readyState==4){ 
document.getElementById(gElem).innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject){
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top