Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable passage from an onChange event function

Status
Not open for further replies.

jackyn

Programmer
Dec 24, 2002
11
FR
hello
Could you help me,anticipate thanks Jack
Here is my Javascript,and my questions:
Why the global variables in the to_do() function
(j and toto) can be used only in the to_do()
function,and when they are external to the to_do()
function,they are declared undefined ,what I have
to do to see these variables in the whole script?

Why in the to_do() function, if I call an other
function (myfonc() ),it's not work, but the
alert(" ") function works correctly ?

<html>
<head>
<title> essai </title>
</head>

<body>
<script language = &quot;JavaScript&quot;>
var toto,j; // global
var streams = new Array();
streams[0] = new makeStream(&quot;&quot;,&quot;&quot;,&quot;&quot;);
streams[1] = new makeStream(&quot;1&quot;,&quot;11&quot;,&quot;table1&quot;);
streams[2] = new makeStream(&quot;2&quot;,&quot;22&quot;,&quot;table2&quot;);

function makeStream(name1,name2,name)
{
this.name1 = name1;
this.name2 = name2;
this.name = name;
}

function to_do(s)
{
toto = s.options[s.selectedIndex].value;

if(toto == &quot;1&quot;)
{
document.write(&quot;TABLE1_fonction&quot;); // ok
j=1;
myfonc(); // not ok
}
if(toto == &quot;2&quot;)
{
//document.write(&quot;TABLE2_fonction&quot;);
j = 2;
alert(&quot;toto&quot;); //ok
}
}

document.writeln('<SELECT NAME=&quot;streams&quot; onChange=&quot;to_do(this)&quot;>');

document.writeln('<SELECT NAME=&quot;streams&quot; onChange=&quot;faire(this)&quot;>');
document.writeln('<OPTION VALUE=&quot;', streams[0].name1, '&quot;>', streams[0].name);
document.writeln('<OPTION VALUE=&quot;', streams[1].name1, '&quot;>', streams[1].name);
document.writeln('<OPTION VALUE=&quot;', streams[2].name1, '&quot;>', streams[2].name);
document.writeln('</SELECT>');

myfonc(x)
{
document.write(&quot;j:&quot;,j);} // not ok, j undefined
document.write(&quot;toto:&quot;,toto); // not ok, toto undefined
}

document.write(&quot;toto:&quot;,toto); // not ok, toto undefined ?
</script>
</body>
</html>
 
Your my_fonc function is expecting you to pass it a value of some sort (x). Try calling it like this and see what happens: my_fonc(&quot;a string&quot;) or you could just take the 'x' out of your function declaration.

-Tarwn Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 

thanks for answering

myfonc()
{
document.write(&quot;j:&quot;,j);} // not ok, j undefined
document.write(&quot;toto:&quot;,toto); // not ok, toto undefined
}

does not work !
j and toto are global
 
I see a couple of issues here. First the definition for myfonc is incomplete and has a typo in it.

function myfonc()
{
document.write(&quot;j:&quot;,j); // not ok, j undefined
document.write(&quot;toto:&quot;,toto); // not ok, toto undefined
}

notice the function and that I removed an extra close brace after the first line.

Also, remove the second document.write(&quot;<SELECT ... as it is not needed.

Now to the logical flow. when the code executes for the first time, on page load, neither j nor toto has any value assigned, hence the 'undefined' when you write toto in the begining.

Once you select something from the drop down list, then both variables get a value.

Here is the modified code. See if it does what you want.

<html>
<head>
<title> essai </title>
</head>

<body>
<script language = &quot;JavaScript&quot;>
var toto,j; // global
var streams = new Array();
streams[0] = new makeStream(&quot;&quot;,&quot;&quot;,&quot;&quot;);
streams[1] = new makeStream(&quot;1&quot;,&quot;11&quot;,&quot;table1&quot;);
streams[2] = new makeStream(&quot;2&quot;,&quot;22&quot;,&quot;table2&quot;);

function makeStream(name1,name2,name)
{
this.name1 = name1;
this.name2 = name2;
this.name = name;
}

function to_do(s)
{
toto = s.options[s.selectedIndex].value;

if(toto == &quot;1&quot;)
{
document.write(&quot;TABLE1_fonction&quot;); // ok
j=1;
myfonc(); // not ok
}
if(toto == &quot;2&quot;)
{
document.write(&quot;TABLE2_fonction&quot;);
j = 2;
alert(&quot;toto&quot;); //ok
}
}

document.writeln('<SELECT NAME=&quot;streams&quot; onChange=&quot;to_do(this)&quot;>');

document.writeln('<OPTION VALUE=&quot;', streams[0].name1, '&quot;>', streams[0].name);
document.writeln('<OPTION VALUE=&quot;', streams[1].name1, '&quot;>', streams[1].name);
document.writeln('<OPTION VALUE=&quot;', streams[2].name1, '&quot;>', streams[2].name);
document.writeln('</SELECT>');

function myfonc()
{
document.write(&quot;j:&quot;,j); // not ok, j undefined
document.write(&quot;toto:&quot;,toto); // not ok, toto undefined
}

document.write(&quot;toto:&quot;,toto); // not ok, toto undefined ?
</script>
</body>
</html>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top