Jump to content

Providing The Id As A Variable


Guest FirefoxRocks
 Share

Recommended Posts

Guest FirefoxRocks

I'm using AJAX in my application to get some subcategories information. I would like to specify the ID of where to return the data to be flexible with the function. I don't know how to do this, I tried adding parenthesis on the function but that broke the functionality.Here is my JavaScript code:

var xmlhttp;function getRows(cat, ele){	xmlhttp = new GetXmlHttpObject();	if (xmlhttp==null){		return;	}		if(cat==="global") {		$("#"+ele).html("<option>(global)</option>");		return;	}		var url="SOMEPAGE.php" + "?cat=" + cat + "&sid=" + Math.random();	xmlhttp.onreadystatechange=stateChanged;	xmlhttp.open("GET",url,true);	xmlhttp.send(null);}function stateChanged(){	if (xmlhttp.readyState===4){		$("#assoc_table_item").html(xmlhttp.responseText);	}}

The variable ele is the ID of the element I want to return the server response to. I don't know how to pass this variable to the stateChanged function, where I can just modify 1 line. With the code I have right now I have to write the stateChanged() function each time I want to change the ID of the element.I tried changing xmlhttp.onreadystatechange=stateChanged; toxmlhttp.onreadystatechange=stateChanged(ele); but it breaks the function.How do I do this?

Link to comment
Share on other sites

Guest FirefoxRocks

That seems to make sense, but now...Firebug output:

onreadystatechange FAILS Syntax error, unrecognized expression: #[object Event] Syntax error, unrecognized expression: #[object Event] [xpconnect wrapped nsIDOMEventListener]
EDIT: Never mind, I passed the variable too many times. I feel like an idiot in both cases :). Thanks for the help. Edited by FirefoxRocks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...