Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Pass a local variable to another function

Reply
Thread Tools

Pass a local variable to another function

 
 
madmikeproductions@gmail.com
Guest
Posts: n/a
 
      01-29-2008
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[i].checked==true) {
tSelect = document.pickerform.chooser[i].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
 
Reply With Quote
 
 
 
 
Kailash Nadh
Guest
Posts: n/a
 
      01-29-2008
On Jan 29, 12:03 am, (E-Mail Removed) 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[i].checked==true) {
> tSelect = document.pickerform.chooser[i].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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-29-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i].checked==true) {
> tSelect = document.pickerform.chooser[i].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_not...ml#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[i].checked==true)
{
var tSelect = document.pickerform.chooser[i].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"][i].checked==true)
{
var tSelect =
document.forms["pickerform"].elements["chooser"][i].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[i].checked)
{
var tSelect = es[i].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
 
Reply With Quote
 
madmikeproductions@gmail.com
Guest
Posts: n/a
 
      01-29-2008
On Jan 28, 7:18 pm, Kailash Nadh <(E-Mail Removed)> wrote:
> On Jan 29, 12:03 am, (E-Mail Removed) 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[i].checked==true) {
> > tSelect = document.pickerform.chooser[i].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
 
Reply With Quote
 
madmikeproductions@gmail.com
Guest
Posts: n/a
 
      01-29-2008
On Jan 28, 9:32 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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[i].checked==true) {
> > tSelect = document.pickerform.chooser[i].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_not...m/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[i].checked==true)
> {
> var tSelect = document.pickerform.chooser[i].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"][i].checked==true)
> {
> var tSelect =
> document.forms["pickerform"].elements["chooser"][i].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[i].checked)
> {
> var tSelect = es[i].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[i].checked==true) {
tSelect = document.pickerform.chooser[i].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
 
Reply With Quote
 
madmikeproductions@gmail.com
Guest
Posts: n/a
 
      01-29-2008
On Jan 29, 9:01 am, (E-Mail Removed) wrote:
> On Jan 28, 9:32 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
> wrote:
>
>
>
> > (E-Mail Removed) 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[i].checked==true) {
> > > tSelect = document.pickerform.chooser[i].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_not...s1DontWorkhttp...

>
> > 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[i].checked==true)
> > {
> > var tSelect = document.pickerform.chooser[i].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"][i].checked==true)
> > {
> > var tSelect =
> > document.forms["pickerform"].elements["chooser"][i].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[i].checked)
> > {
> > var tSelect = es[i].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[i].checked==true) {
> tSelect = document.pickerform.chooser[i].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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass C++ object to another C++ function via Python function grbgooglefan Python 9 05-09-2008 01:34 PM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
Trying to pass values of variables from a redefined function into another function deanfamily C++ 4 10-23-2005 11:36 PM
Pass Variable to another function manmit.walia@gmail.com ASP .Net 3 10-14-2005 12:00 AM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments