...

View Full Version : Assigning Value to Global Variable in a Function



ollie_mo
10-05-2011, 01:55 PM
I am trying to figure out how to assign a value to a global variable within a function and can't seem to figure it out. Here's my thought,



<script type="text/javascript">
var global1=""; var global2="";

function assign(vari,strng){
vari = strng;
}
</script>...

<input name="box1" onblur="assign('global1',this.value)"/>
<input name="box2" onblur="assign('global2',this.value)"/>
...


The purpose behind this is creating a form that will work with an existing database that would normally have a text area with lots of information. I am trying to turn it into a checklist that I can run from a mobile device. The global variables woudl be used to fill in a hidden text area that would then be passed on to the database upon submission. I am trying to keep the code as compact as possible. Any ideas?

Philip M
10-05-2011, 02:08 PM
<script type="text/javascript">
var global1="";
var global2="";

function assign(vari,strng){
if (vari == "1") {
global1 = strng;
}
if (vari == "2") {
global2 = strng;
}
alert (global1 + " " + global2);
}
</script>

<input name="box1" onblur="assign('1',this.value)"/>
<input name="box2" onblur="assign('2',this.value)"/>


"Most of the time I don't have much fun. The rest of the time I don't have any fun at all." -Woody Allen - US movie actor, comedian, & director (1935 - )

ollie_mo
10-05-2011, 02:16 PM
I thought about that. Problem is I am trying to find a way to do it without calling each variable specifically in the function, let the parameter define which variable is updated. There will be many variables and I'd like to keep the functioin as light as possible.

Philip M
10-05-2011, 02:43 PM
You did not say before that "there will be many variables". Why not use an array?



<script type="text/javascript">
var global = [];
function assign(vari,strng){
global[vari] = strng;
alert (global[0] + " " + global[1]);
}
</script>

<input name="box1" onblur="assign('0',this.value)"/>
<input name="box2" onblur="assign('1',this.value)"/>


If you wish you can initialise the array elements with


for (var i=0; i<100; i++) {
global[i]="";
}

ollie_mo
10-05-2011, 03:07 PM
That might work, thanks. Don't suppose there is a way to pull the parameter value into the function so that it actually defines the name of the variable I want to change. I posted a sample just to get the idea across, but variable names are really more like bodyStyle, patAwn, winAwn etc. Keeping with descriptive variable names to make better sense of the code. I like the array idea, I might work with that if I can't come up with another way. The idea is to be able to pass something like:


function updateExterior(){
var exterior = bodyStyle + patAwn + winAwn;
document.form.exter.innerHTML=exterior;
}
....

<textarea name="exter" style="visibility:hidden;"></textarea>

DaveyErwin
10-05-2011, 03:24 PM
I am trying to figure out how to assign a value to a global variable within a function and can't seem to figure it out. Here's my thought,



<script type="text/javascript">
var global1=""; var global2="";

function assign(vari,strng){
vari = strng;
}
</script>...

<input name="box1" onblur="assign('global1',this.value)"/>
<input name="box2" onblur="assign('global2',this.value)"/>
...


The purpose behind this is creating a form that will work with an existing database that would normally have a text area with lots of information. I am trying to turn it into a checklist that I can run from a mobile device. The global variables woudl be used to fill in a hidden text area that would then be passed on to the database upon submission. I am trying to keep the code as compact as possible. Any ideas?

This is does what you are
trying to do but, you realy
should rethink your approach
to the problem . Try using
an array as Philip M
suggested.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">
var global1=""; var global2="";

function assign(vari,strng){
window[vari] = strng;
}

</script>
</head>
<body>
<input name="box1" onblur="assign('global1',this.value);alert(global1)"/>
<input name="box2" onblur="assign('global2',this.value);alert(global2)"/>
...
</div>
</html>

ollie_mo
10-05-2011, 03:40 PM
This is exactly what I needed, thank you. I understand that an array would be cleaner and more technically correct, but it does not allow for descriptive variable names. The text area that this code will help populate will consist of values from between 5 and 20 small text areas, checkboxes and radio buttons. Form elements that I can easily manipulate from a mobile device that will have pre-existing options to fill a description.

devnull69
10-05-2011, 04:19 PM
Just for my understanding ... why not like this?


<input name="box1" onblur="global1=this.value;alert(global1)"/>

What is the benefit of using the "detour" via a function and a string variable name??

DaveyErwin
10-05-2011, 04:24 PM
This is exactly what I needed, thank you. I understand that an array would be cleaner and more technically correct, but it does not allow for descriptive variable names. The text area that this code will help populate will consist of values from between 5 and 20 small text areas, checkboxes and radio buttons. Form elements that I can easily manipulate from a mobile device that will have pre-existing options to fill a description.

Okay then doit like this ....


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">

var myObj={ global1:"", global2:""};

function assign(vari,strng){
myObj[vari] = strng;
}

</script>
</head>
<body>
<input name="box1" onblur="assign('global1',this.value);alert(myObj.global1)"/>
<input name="box2" onblur="assign('global2',this.value);alert(myObj.global2)"/>
...
</div>
</html>

But I still think Philip M
and devnull69
are on a better track.

ollie_mo
10-05-2011, 07:03 PM
Just for my understanding ... why not like this?


<input name="box1" onblur="global1=this.value;alert(global1)"/>

What is the benefit of using the "detour" via a function and a string variable name??

Now that is perfect. I didn't even think of directly defining it that way.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum