passing var from <head> to <body>

F

Frances

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..
 
F

Frances

Zoe said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user selects
in sel obj.. thank you..


if the content is dynamic you will need to create a function that creates
the text and an event to trigger it. for example the onChange event of your
select could call a function call writeDynamicContent() which then spits out
the text in the <div>.
yes, I'm calling doIt() (function that writes dynamic content) with
onChange Event handler in sel obj.. but still content doesn't print
where I want it to b/c var isn't being passed from function to where I
want content to print... how DO you pass a variable from inside a
function to outside it? thank you very much for yr help...
 
L

Lee

Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..

It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.
 
M

Mick White

Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


list = document.forms[0].product; // Now "list" is global.

Mick

[snip]
 
C

Christopher Benson-Manica

Lee said:
Removing the "var" keyword from the declaration will make
the variable global.

My personal preference is to explicitly declare global variables as
such.
 
F

Frances

Mick said:
Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


list = document.forms[0].product; // Now "list" is global.
and where is this var declaration? if I put it outside function then
function can't read it.. if I put it inside function then it can't be
read outside function.. what am I missing here? how do you OUTSIDE
functions var's you have processed inside functions? thank you very
much.. again here is situation:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<select name="product" onChange="doIt()"> ... </select>
[...............]

<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass var from function to here?
</script>
</body>
 
L

Lee

Frances said:
Mick said:
Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


list = document.forms[0].product; // Now "list" is global.
and where is this var declaration? if I put it outside function then
function can't read it..

Where did you get that idea? A var declared outside of any function
is global. You can also simply leave off the "var" keyword.
 
L

Lee

Christopher Benson-Manica said:
My personal preference is to explicitly declare global variables as
such.

That would be nice, if there was a way to do so in Javascript.
 
F

Frances

Lee said:
Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..


It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.

oh gosh, I see, it's like java then.....:)

so this will make it global:

var list;
var selItem;
function doIt() {
list = document.forms[0].product;
selItem = list.options[list.selectedIndex].value;
}

thank you all very much for your responses......
 
R

Randy Webb

Frances said the following on 9/19/2005 1:30 PM:
Lee said:
Frances said:
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..



It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.

oh gosh, I see, it's like java then.....:)

so this will make it global:

var list;
var selItem;
function doIt() {
list = document.forms[0].product;
selItem = list.options[list.selectedIndex].value;
}

thank you all very much for your responses......

This makes it global also:

list = '';
selItem = '';

But your problem seems to be that you are trying to access a variable
that is inside a function but you are trying to access it before the
functions is executed. That means the variable has not been defined, nor
created yet, so you get the error.
 
L

Lee

Christopher Benson-Manica said:
<html><script>
var foo;
</script></html>

What is the scope of foo?

It's quite a stretch to call that "declaring it as global".
There is nothing about the statement that makes it global,
simply the fact that the statement appears in a global scope.

In production code, I find it best to simply name globals
appropriately, regardless of where they are first used:

function popup() {
globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
...
}
 
C

Christopher Benson-Manica

Lee said:
It's quite a stretch to call that "declaring it as global".
There is nothing about the statement that makes it global,
simply the fact that the statement appears in a global scope.

That's all I really intended to imply. Maybe

window['foo']='foo';

?
In production code, I find it best to simply name globals
appropriately, regardless of where they are first used:
function popup() {
globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
...
}

I'd probably name it gPopupHTML, if I were to take that route.
 
W

web.dev

Frances said:
<html>
<head>
<script>
Don't forget to include the type attribute:

function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^

Have the selItem as a global variable:

var selItem = "";

function doIt()
{
...code statements...
selItem = list.option[list.selectedIndex].value;
...code statements...
}

Don't forget to have a closing script tag:

</head>


<body>
<script>

document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?

Now you can use the selItem variable here since it was declared global.
 
R

Randy Webb

web.dev said the following on 9/19/2005 7:31 PM:
Frances said:
<html>
<head>
<script>

Don't forget to include the type attribute:

function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^


Have the selItem as a global variable:

var selItem = "";

function doIt()
{
...code statements...
selItem = list.option[list.selectedIndex].value;
...code statements...
}

Don't forget to have a closing script tag:

</head>


<body>
<script>


document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?


Now you can use the selItem variable here since it was declared global.

But it will blank, as you defined it as "". The desired (I am assuming)
effect is to have the list.option[list.selectedIndex].value placed where
they have the document.write statement.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top