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,
Code:
<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?
<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 - )
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
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.
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:
Code:
function updateExterior(){
var exterior = bodyStyle + patAwn + winAwn;
document.form.exter.innerHTML=exterior;
}
....
<textarea name="exter" style="visibility:hidden;"></textarea>
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,
Code:
<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.
Code:
<!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>
Last edited by DaveyErwin; 10-05-2011 at 03:32 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.
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 ....
Code:
<!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.
Last edited by DaveyErwin; 10-05-2011 at 04:28 PM..