Pass a local variable to another function

M

madmikeproductions

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
 
K

Kailash Nadh

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);
}
 
T

Thomas 'PointedEars' Lahn

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
 
M

madmikeproductions

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,
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
 
M

madmikeproductions

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
 
M

madmikeproductions

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.

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") ...
}

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top