Link to home
Start Free TrialLog in
Avatar of JAaron Anderson
JAaron AndersonFlag for United States of America

asked on

HowTo set a global variable in javascript or jQuery

hi all, Ive read alot on the web that simply doesnt make sense on this hoping to have a clear simple discussion on how to handle a string and perform comparison against it along the open session...

e.g.
top of page something like setting default state like so:
<script language="javascript">
var globalSessionValue="false";
</script>
... then many MANY lines later a later function changes the value of the string like so:
<script language="javascript">
function whatever(){
if ( getElementbyID("WhateveruserEvent").value ... whatever comparison here <= === ! > 0) {
globalSessionValue="true";
}
</script>
...
so this way later I can perform another simple if/else on the changed state
<script type="text/javascript">
function evaluateRestriction(){ // this has tested to fire On TAB into the field CONFIRMED
var checkState = globalSessionValue.value; 
		if ( globalSessionValue.length > 4) { //false
			document.getElementById("LogicContainer").innerHTML = "<img src='SHOW GOOD PHOTO' />";
		} else { 
			document.getElementById("LogicContainer").innerHTML = ""; // DONT SHOW anything here

		}	
	}
</script>

<div id="LogicContainer">

</div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAaron Anderson

ASKER

thx Julian
>>What is your thinking here?
I was trying to declare some sort of interface that carries in the DOM or somwehre I can call back to and get the value of a targeted memory thread.
Ive read if you write "var" in front of a primitive variable it sets it "higher" potentially as a global state ?
anyways with ".value" Im simply trying to access the contents of the container and not the memory thread accessor itself. hth
ok thx Ill try the boolean operation
var - is global to the scope it is defined at.
<script>
var superGlobal = true; //Visible everywhere
function test() 
{
   var localGlobal = false; // Global to function test
}
</script>

Open in new window


To use globalSessionValue as an object you would have to do something like this

var globalSessionValue = {
   value: false;
}

Open in new window


Now you can talk about globalSession.value;