dscxz dscxz - 4 months ago 20
Javascript Question

i can't call 2 functions from onchange event

I have to call an ajax function as well as call an additional function to get another value by giving the above text field as the input. I want to get the return ,form that function.
Here is the code I have used to call the 2 functions, but only the first function ajax(this) is only working, I am getting the return after calling the ajax function whereas I am not getting any return value for the second function. What I am doing wrong
here is my code

<select style="width: 305px;" name="category_id1" id="category_id1" onchange="(function(){ajax('this');return locname('this');})(this)">


my functions are

function ajax(control)
{
var loc=document.getElementById('category_id1').value;
//alert(loc);
var req;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "ajax.php?&loc="+loc+"", true);
req.send();

req.onreadystatechange=function(){
if(req.readyState==4&&req.status==200){
//$(".error").hide();
result=req.responseText
alert(result);
var items = JSON.parse(result);
//alert(items.length)
var html = '<option value="">Select</option>';
for ( var i = 0; i < items.length; i++ ) {
html += "<option value='" + items[ i ] + "'>" + items[ i ] + "</option>"
}
document.getElementById("Ultra").innerHTML = html;
}
}
}
function locname(control)
{
var locName=control.value;
//alert(locName)
var json = <?php echo $response ?>;
var orgName=document.getElementById("category_id").value;
//alert(json);
var html="<option>Select</option>";
var locations;
//alert(json.length);
for(var i=0; i<json.length; i++)
{
var item = json[i].name;
//alert(item);
if(orgName===item){
//alert(item)
locations=json[i].location;
for(var j=0;j<locations.length;j++){
var location=locations[j].name;
//alert(locName)
if(locName===location)
{
//alert(locName);
var buildings=locations[j].building
//alert(buildings)
for(var k=0;k<buildings.length;k++)
{
var building=buildings[k];
//alert(building);
html=html+"<option value='"+building+"'>"+building+"</option>";
}
}

}

}
}
document.getElementById("category_id2").innerHTML=html;
}

hsz hsz
Answer

Just try with:

onchange="ajax(this);locname(this);"
Comments