Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post

    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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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]="";
    }
    Last edited by Philip M; 10-05-2011 at 02:46 PM. Reason: Typo

    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.

  • Users who have thanked Philip M for this post:

    ollie_mo (10-05-2011)

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post
    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>

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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.

  • Users who have thanked DaveyErwin for this post:

    ollie_mo (10-05-2011)

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 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.

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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??

  • Users who have thanked devnull69 for this post:

    ollie_mo (10-05-2011)

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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.

  • #10
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •