Pass a local variable to another function

Discussion in 'Javascript' started by madmikeproductions@gmail.com, Jan 29, 2008.

  1. Guest

    How do I pass a local variable from one function to another function?
    Is there a way to send that data to a global variable and re-use it?
    Here is what I am trying to do:
    I have a color picker form that has radio buttons that tell the form
    which text field to write to.
    When you select the radio button, it should put the value of that
    radio button in a variable so that the function of the color picker
    can write the color code to the textbox that is selected.
    Here is the basis of the code:

    function elem_check() {
    for (i=0; i<document.pickerform.chooser.length; i++) {
    if (document.pickerform.chooser.checked==true) {
    tSelect = document.pickerform.chooser.value
    }
    }
    //I either then need to pass tSelect to the other function or
    write this string to it ("$('"+tSelect+"').value='#'+v;");
    }

    Anytime I've tried doing this globally without the function, it
    doesn't work and anytime I try to call tSelect to the other function,
    it only works on the first textbox.

    Thanks for any help.

    Michael M. Kadrie
    Atlas Design Group
    imagine it Real
     
    , Jan 29, 2008
    #1
    1. Advertising

  2. Kailash Nadh Guest

    On Jan 29, 12:03 am, wrote:
    > How do I pass a local variable from one function to another function?
    > Is there a way to send that data to a global variable and re-use it?
    > Here is what I am trying to do:
    > I have a color picker form that has radio buttons that tell the form
    > which text field to write to.
    > When you select the radio button, it should put the value of that
    > radio button in a variable so that the function of the color picker
    > can write the color code to the textbox that is selected.
    > Here is the basis of the code:
    >
    > function elem_check() {
    > for (i=0; i<document.pickerform.chooser.length; i++) {
    > if (document.pickerform.chooser.checked==true) {
    > tSelect = document.pickerform.chooser.value
    > }
    > }
    > //I either then need to pass tSelect to the other function or
    > write this string to it ("$('"+tSelect+"').value='#'+v;");
    >
    > }
    >
    > Anytime I've tried doing this globally without the function, it
    > doesn't work and anytime I try to call tSelect to the other function,
    > it only works on the first textbox.
    >
    > Thanks for any help.
    >
    > Michael M. Kadrie
    > Atlas Design Group
    > imagine it Real


    Unless I am missing something, all you would need to do to pass
    tSelect to another function is

    function elem_check() {
    // your code..

    otherFunction(tSelect);
    }

    --
    Kailash Nadh | http://kailashnadh.name
     
    Kailash Nadh, Jan 29, 2008
    #2
    1. Advertising

  3. wrote:
    > How do I pass a local variable from one function to another function?
    > Is there a way to send that data to a global variable and re-use it?
    > [...]
    > function elem_check() {
    > for (i=0; i<document.pickerform.chooser.length; i++) {
    > if (document.pickerform.chooser.checked==true) {
    > tSelect = document.pickerform.chooser.value
    > }
    > }
    > //I either then need to pass tSelect to the other function or
    > write this string to it ("$('"+tSelect+"').value='#'+v;");
    > }
    >
    > Anytime I've tried doing this globally without the function, it
    > doesn't work and anytime I try to call tSelect to the other function,
    > it only works on the first textbox.


    http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork
    http://www.jibbering.com/faq/#FAQ4_43

    First of all, you have no local variables here. Instead you create a number
    of properties of an object in the scope chain, probably the Global Object.
    For an identifier to become a local variable, you have to use a variable
    declaration:

    function elem_check()
    {
    for (var i = 0; i < document.pickerform.chooser.length; i++)
    {
    if (document.pickerform.chooser.checked==true)
    {
    var tSelect = document.pickerform.chooser.value
    }

    // ...
    }
    }

    Second, your referencing is proprietary and error-prone. Use instead

    function elem_check()
    {
    for (var i = 0;
    i < document.forms["pickerform"].elements["chooser"].length;
    i++)
    {
    if (document.forms["pickerform"].elements["chooser"].checked==true)
    {
    var tSelect =
    document.forms["pickerform"].elements["chooser"].value;
    }

    // ...
    }
    }

    Third, this can be optimized quite a bit:

    function elem_check()
    {
    var es = document.forms["pickerform"].elements["chooser"];

    for (var i = 0, len = es.length; i < len; i++)
    {
    if (es.checked)
    {
    var tSelect = es.value;
    break;
    }
    }

    // ...
    }

    As for your question, if that still is a problem after these modifications,
    the best approach is to use real local variables and pass the variable value
    to the other function.

    However, when that is not an option, you can get a reference to the Global
    Object of which global variables are properties (with the DontDelete attribute):

    var _global = this;
    var tSelect;

    function foo(sIdent)
    {
    // ... _global[sIdent] ...
    }

    function elem_check()
    {
    // tSelect = ...
    // ... foo("tSelect") ...
    }


    HTH

    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Jan 29, 2008
    #3
  4. Guest

    On Jan 28, 7:18 pm, Kailash Nadh <> wrote:
    > On Jan 29, 12:03 am, wrote:
    >
    >
    >
    > > How do Ipassalocalvariablefrom one function to another function?
    > > Is there a way to send that data to a globalvariableand re-use it?
    > > Here is what I am trying to do:
    > > I have a color picker form that has radio buttons that tell the form
    > > which text field to write to.
    > > When you select the radio button, it should put the value of that
    > > radio button in avariableso that the function of the color picker
    > > can write the color code to the textbox that is selected.
    > > Here is the basis of the code:

    >
    > > function elem_check() {
    > > for (i=0; i<document.pickerform.chooser.length; i++) {
    > > if (document.pickerform.chooser.checked==true) {
    > > tSelect = document.pickerform.chooser.value
    > > }
    > > }
    > > //I either then need topasstSelect to the other function or
    > > write this string to it ("$('"+tSelect+"').value='#'+v;");

    >
    > > }

    >
    > > Anytime I've tried doing this globally without the function, it
    > > doesn't work and anytime I try to call tSelect to the other function,
    > > it only works on the first textbox.

    >
    > > Thanks for any help.

    >
    > > Michael M. Kadrie
    > > Atlas Design Group
    > > imagine it Real

    >
    > Unless I am missing something, all you would need to do topass
    > tSelect to another function is
    >
    > function elem_check() {
    > // your code..
    >
    > otherFunction(tSelect);
    >
    > }
    >
    > --
    > Kailash Nadh |http://kailashnadh.name


    Kailash,
    Actually I didn't incorporate the other function which is a function
    of the color picker, but it already has arguments assigned to it and
    if I add to the arguments, it seems to break the code. So it's a
    little more complicated than that.
    Thanks though,
    Mike
     
    , Jan 29, 2008
    #4
  5. Guest

    On Jan 28, 9:32 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > wrote:
    > > How do I pass a local variable from one function to another function?
    > > Is there a way to send that data to a global variable and re-use it?
    > > [...]
    > > function elem_check() {
    > > for (i=0; i<document.pickerform.chooser.length; i++) {
    > > if (document.pickerform.chooser.checked==true) {
    > > tSelect = document.pickerform.chooser.value
    > > }
    > > }
    > > //I either then need to pass tSelect to the other function or
    > > write this string to it ("$('"+tSelect+"').value='#'+v;");
    > > }

    >
    > > Anytime I've tried doing this globally without the function, it
    > > doesn't work and anytime I try to call tSelect to the other function,
    > > it only works on the first textbox.

    >
    > http://www.jibbering.com/faq/faq_no...DontWorkhttp://www.jibbering.com/faq/#FAQ4_43
    >
    > First of all, you have no local variables here. Instead you create a number
    > of properties of an object in the scope chain, probably the Global Object.
    > For an identifier to become a local variable, you have to use a variable
    > declaration:
    >
    > function elem_check()
    > {
    > for (var i = 0; i < document.pickerform.chooser.length; i++)
    > {
    > if (document.pickerform.chooser.checked==true)
    > {
    > var tSelect = document.pickerform.chooser.value
    > }
    >
    > // ...
    > }
    > }
    >
    > Second, your referencing is proprietary and error-prone. Use instead
    >
    > function elem_check()
    > {
    > for (var i = 0;
    > i < document.forms["pickerform"].elements["chooser"].length;
    > i++)
    > {
    > if (document.forms["pickerform"].elements["chooser"].checked==true)
    > {
    > var tSelect =
    > document.forms["pickerform"].elements["chooser"].value;
    > }
    >
    > // ...
    > }
    > }
    >
    > Third, this can be optimized quite a bit:
    >
    > function elem_check()
    > {
    > var es = document.forms["pickerform"].elements["chooser"];
    >
    > for (var i = 0, len = es.length; i < len; i++)
    > {
    > if (es.checked)
    > {
    > var tSelect = es.value;
    > break;
    > }
    > }
    >
    > // ...
    > }
    >
    > As for your question, if that still is a problem after these modifications,
    > the best approach is to use real local variables and pass the variable value
    > to the other function.
    >
    > However, when that is not an option, you can get a reference to the Global
    > Object of which global variables are properties (with the DontDelete attribute):
    >
    > var _global = this;
    > var tSelect;
    >
    > function foo(sIdent)
    > {
    > // ... _global[sIdent] ...
    > }
    >
    > function elem_check()
    > {
    > // tSelect = ...
    > // ... foo("tSelect") ...
    > }
    >
    > HTH
    >
    > PointedEars
    > --
    > var bugRiddenCrashPronePieceOfJunk = (
    > navigator.userAgent.indexOf('MSIE 5') != -1
    > && navigator.userAgent.indexOf('Mac') != -1
    > ) // Plone, register_function.js:16


    PE,
    Thanks for the advice.
    Still not working quite right.
    Here is the full script that I have for the functions (original - have
    not simplified it yet).
    *********************************************
    //Begin Code
    *********************************************
    function $(v) { return(document.getElementById(v)); }
    function $S(v) { return(document.getElementById(v).style); }

    var maxValue={'h':360,'s':100,'v':100}, HSV={0:360,1:100,2:100};
    var hSV=165, wSV=162, hH=163, slideHSV={0:360,1:100,2:100}, zINDEX=15,
    stop=1;

    //This function is to select a text box from a radio button so that
    the color code will be put into that text box
    function elem_check() {
    for (i=0; i<document.pickerform.chooser.length; i++) {
    if (document.pickerform.chooser.checked==true) {
    tSelect = document.pickerform.chooser.value
    }
    }
    //then I either need to put tSelect into a gloabal variable or
    put this string into the variable "$('"+tSelect+"').value='#'+v;"
    }

    //This is the code that is for the color picker
    function HSVupdate(v) {
    v=hsv2hex(HSV=v?v:slideHSV);
    $('plugHEX').innerHTML=v;
    $S('plugCUR').background='#'+v;
    $S('plugID').background='#'+v;
    //Next line of code, I put in to get the value in to a text
    box for testing - it works.
    $('hex_temp').value='#'+v;
    //This is where I need to return the value of tSelect or the string
    from above.
    //I've tried $(tSelect).value='#'+v; but tSelect is not a
    global variable

    return(v);

    }

    ************************************************
    //end of code
    ************************************************
    There is other code besides these functions, but they are unrelated to
    these.
    Thanks again for your help and insight.
    Mike
     
    , Jan 29, 2008
    #5
  6. Guest

    On Jan 29, 9:01 am, wrote:
    > On Jan 28, 9:32 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    >
    >
    >
    > > wrote:
    > > > How do I pass a local variable from one function to another function?
    > > > Is there a way to send that data to a global variable and re-use it?
    > > > [...]
    > > > function elem_check() {
    > > > for (i=0; i<document.pickerform.chooser.length; i++) {
    > > > if (document.pickerform.chooser.checked==true) {
    > > > tSelect = document.pickerform.chooser.value
    > > > }
    > > > }
    > > > //I either then need to pass tSelect to the other function or
    > > > write this string to it ("$('"+tSelect+"').value='#'+v;");
    > > > }

    >
    > > > Anytime I've tried doing this globally without the function, it
    > > > doesn't work and anytime I try to call tSelect to the other function,
    > > > it only works on the first textbox.

    >
    > >http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWorkhttp...

    >
    > > First of all, you have no local variables here. Instead you create a number
    > > of properties of an object in the scope chain, probably the Global Object.
    > > For an identifier to become a local variable, you have to use a variable
    > > declaration:

    >
    > > function elem_check()
    > > {
    > > for (var i = 0; i < document.pickerform.chooser.length; i++)
    > > {
    > > if (document.pickerform.chooser.checked==true)
    > > {
    > > var tSelect = document.pickerform.chooser.value
    > > }

    >
    > > // ...
    > > }
    > > }

    >
    > > Second, your referencing is proprietary and error-prone. Use instead

    >
    > > function elem_check()
    > > {
    > > for (var i = 0;
    > > i < document.forms["pickerform"].elements["chooser"].length;
    > > i++)
    > > {
    > > if (document.forms["pickerform"].elements["chooser"].checked==true)
    > > {
    > > var tSelect =
    > > document.forms["pickerform"].elements["chooser"].value;
    > > }

    >
    > > // ...
    > > }
    > > }

    >
    > > Third, this can be optimized quite a bit:

    >
    > > function elem_check()
    > > {
    > > var es = document.forms["pickerform"].elements["chooser"];

    >
    > > for (var i = 0, len = es.length; i < len; i++)
    > > {
    > > if (es.checked)
    > > {
    > > var tSelect = es.value;
    > > break;
    > > }
    > > }

    >
    > > // ...
    > > }

    >
    > > As for your question, if that still is a problem after these modifications,
    > > the best approach is to use real local variables and pass the variable value
    > > to the other function.

    >
    > > However, when that is not an option, you can get a reference to the Global
    > > Object of which global variables are properties (with the DontDelete attribute):

    >
    > > var _global = this;
    > > var tSelect;

    >
    > > function foo(sIdent)
    > > {
    > > // ... _global[sIdent] ...
    > > }

    >
    > > function elem_check()
    > > {
    > > // tSelect = ...
    > > // ... foo("tSelect") ...
    > > }

    >
    > > HTH

    >
    > > PointedEars
    > > --
    > > var bugRiddenCrashPronePieceOfJunk = (
    > > navigator.userAgent.indexOf('MSIE 5') != -1
    > > && navigator.userAgent.indexOf('Mac') != -1
    > > ) // Plone, register_function.js:16

    >
    > PE,
    > Thanks for the advice.
    > Still not working quite right.
    > Here is the full script that I have for the functions (original - have
    > not simplified it yet).
    > *********************************************
    > //Begin Code
    > *********************************************
    > function $(v) { return(document.getElementById(v)); }
    > function $S(v) { return(document.getElementById(v).style); }
    >
    > var maxValue={'h':360,'s':100,'v':100}, HSV={0:360,1:100,2:100};
    > var hSV=165, wSV=162, hH=163, slideHSV={0:360,1:100,2:100}, zINDEX=15,
    > stop=1;
    >
    > //This function is to select a text box from a radio button so that
    > the color code will be put into that text box
    > function elem_check() {
    > for (i=0; i<document.pickerform.chooser.length; i++) {
    > if (document.pickerform.chooser.checked==true) {
    > tSelect = document.pickerform.chooser.value
    > }
    > }
    > //then I either need to put tSelect into a gloabal variable or
    > put this string into the variable "$('"+tSelect+"').value='#'+v;"
    >
    > }
    >
    > //This is the code that is for the color picker
    > function HSVupdate(v) {
    > v=hsv2hex(HSV=v?v:slideHSV);
    > $('plugHEX').innerHTML=v;
    > $S('plugCUR').background='#'+v;
    > $S('plugID').background='#'+v;
    > //Next line of code, I put in to get the value in to a text
    > box for testing - it works.
    > $('hex_temp').value='#'+v;
    > //This is where I need to return the value of tSelect or the string
    > from above.
    > //I've tried $(tSelect).value='#'+v; but tSelect is not a
    > global variable
    >
    > return(v);
    >
    > }
    >
    > ************************************************
    > //end of code
    > ************************************************
    > There is other code besides these functions, but they are unrelated to
    > these.
    > Thanks again for your help and insight.
    > Mike


    Thanks everyone.
    Got it worked out.
    I implemented PE's short code and declared tSelect outside of the
    function and it worked.
    Beautiful people.
     
    , Jan 29, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    555
  2. Ben Kial
    Replies:
    1
    Views:
    659
    Eric Enright
    Nov 15, 2004
  3. deanfamily
    Replies:
    4
    Views:
    413
    puzzlecracker
    Oct 24, 2005
  4. sairam
    Replies:
    2
    Views:
    367
    Steve Holden
    Apr 5, 2007
  5. grbgooglefan
    Replies:
    9
    Views:
    879
    grbgooglefan
    May 9, 2008
Loading...

Share This Page