Jump to content

Sharing variables between functions


pdedecker

Recommended Posts

I want to make prplNmbr, prplExpanded and originalState readable and writeable in all functions, but I don't know how. Here's the code:

<script type="text/javascript">prplExpanded = "no";originalState = "";prplNmbr = "";function showPrplDetails(prplNmbr) {	if (prplExpanded != "no") {		hidePrplDetails(prplExpanded);	}	var prplExpanded = prplNmbr;	var originalState = document.getElementById("prpl" + prplNmbr).innerHTML;	document.getElementById("prpl" + prplNmbr).innerHTML = originalState + "<br />Bezig met laden...";	var detailsRequest = new Ajax.Request(		"../include/getPrplDetails.php",		{			method: "post",			postBody: "id=" + prplNmbr,			onSuccess: setPrplDetails		});}function setPrplDetails(originalRequest) {	document.getElementById('prpl' + prplNmbr).innerHTML = originalState + "<br />" + originalRequest.responseText;}function hidePrplDetails(prplNmbrHide) {	document.getElementById('prpl' + prplNmbrHide).innerHTML = originalState;}</script>

By the way, I've used some script.aculo.us stuff somewhere in the script.

Link to comment
Share on other sites

I tried this:

var prplExpanded = "no";var originalState = "";var prplNmbr = "";function showPrplDetails(prplNmbr) {	if (prplExpanded != "no") {		hidePrplDetails(prplExpanded);	}	prplExpanded = prplNmbr;	originalState = document.getElementById("prpl" + prplNmbr).innerHTML;	document.getElementById("prpl" + prplNmbr).innerHTML = originalState + "<br />Bezig met laden...";	var detailsRequest = new Ajax.Request(		"../include/getPrplDetails.php",		{			method: "post",			postBody: "id=" + prplNmbr,			onSuccess: setPrplDetails		});}function setPrplDetails(originalRequest) {	document.getElementById("prpl" + prplNmbr).innerHTML = originalState + "<br />" + originalRequest.responseText;}function hidePrplDetails(prplNmbrHide) {	document.getElementById('prpl' + prplNmbrHide).innerHTML = originalState;}

setPrplDetails() is still unable to access prplNmbr. When I try to display the value of prplNmbr in a dialog box, it returns no text. If I try this in showPrplDetails() however it works fine.

Link to comment
Share on other sites

Unless I'm missing something, you initialize prplNmbr to the empty string and never change it. So it would seem that printing it with alert would not print any text, the variable is empty. If you initialize it to a non-empty value and test it you should see that it works.

var prplNmbr = "test value";function setPrplDetails() {	//document.getElementById("prpl" + prplNmbr).innerHTML = originalState + "<br />" + originalRequest.responseText;	alert(prplNmbr);}setPrplDetails();

Link to comment
Share on other sites

Unless I'm missing something, you initialize prplNmbr to the empty string and never change it.
You are missing something. :)
function showPrplDetails([b]prplNmbr[/b]) {

But I found a workaround. Instead of doing (for example) showPrplDetails('5'), I just do prplNmbr = 5; showPrplDetails();. And that seems to work fine.

Link to comment
Share on other sites

You are missing something.
That code doesn't change the prplNmbr variable. Let me explain what I was saying. You initialize the variable here:var prplNmbr = "";But it never changes. Yeah, you call the function parameter prplNmbr also, but if you call the function it does not change the global variable. You would need to do that yourself. Name the function parameter something else, and assign its value to the global variable.
function showPrplDetails(val) {  prplNmbr = val;  ...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...