Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2011, 01:55 PM   PM User | #1
ollie_mo
New to the CF scene

 
Join Date: Jul 2011
Location: Missouri
Posts: 9
Thanks: 3
Thanked 1 Time in 1 Post
ollie_mo is an unknown quantity at this point
Assigning Value to Global Variable in a Function

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?
ollie_mo is offline   Reply With Quote
Old 10-05-2011, 02:08 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<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.
Philip M is online now   Reply With Quote
Old 10-05-2011, 02:16 PM   PM User | #3
ollie_mo
New to the CF scene

 
Join Date: Jul 2011
Location: Missouri
Posts: 9
Thanks: 3
Thanked 1 Time in 1 Post
ollie_mo is an unknown quantity at this point
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.
ollie_mo is offline   Reply With Quote
Old 10-05-2011, 02:43 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You did not say before that "there will be many variables". Why not use an array?


Code:
<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

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

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.

Last edited by Philip M; 10-05-2011 at 02:46 PM.. Reason: Typo
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
ollie_mo (10-05-2011)
Old 10-05-2011, 03:07 PM   PM User | #5
ollie_mo
New to the CF scene

 
Join Date: Jul 2011
Location: Missouri
Posts: 9
Thanks: 3
Thanked 1 Time in 1 Post
ollie_mo is an unknown quantity at this point
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>
ollie_mo is offline   Reply With Quote
Old 10-05-2011, 03:24 PM   PM User | #6
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by ollie_mo View Post
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..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
ollie_mo (10-05-2011)
Old 10-05-2011, 03:40 PM   PM User | #7
ollie_mo
New to the CF scene

 
Join Date: Jul 2011
Location: Missouri
Posts: 9
Thanks: 3
Thanked 1 Time in 1 Post
ollie_mo is an unknown quantity at this point
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.
ollie_mo is offline   Reply With Quote
Old 10-05-2011, 04:19 PM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Just for my understanding ... why not like this?
Code:
<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??
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
ollie_mo (10-05-2011)
Old 10-05-2011, 04:24 PM   PM User | #9
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by ollie_mo View Post
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..
DaveyErwin is offline   Reply With Quote
Old 10-05-2011, 07:03 PM   PM User | #10
ollie_mo
New to the CF scene

 
Join Date: Jul 2011
Location: Missouri
Posts: 9
Thanks: 3
Thanked 1 Time in 1 Post
ollie_mo is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
Just for my understanding ... why not like this?
Code:
<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.
ollie_mo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 PM.


Advertisement
Log in to turn off these ads.