By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

passing var from <head> to <body>

P: n/a

<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..
Sep 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

"Frances" <fd***@yahoo.com> wrote in message
news:43*********************@news.sunsite.dk...

<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>.
Sep 19 '05 #2

P: n/a
Zoe Brown wrote:
"Frances" <fd***@yahoo.com> wrote in message
news:43*********************@news.sunsite.dk...
<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...
Sep 19 '05 #3

P: n/a
Frances wrote:

<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]
Sep 19 '05 #4

P: n/a
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.

Sep 19 '05 #5

P: n/a
Lee <RE**************@cox.net> wrote:
Removing the "var" keyword from the declaration will make
the variable global.


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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #6

P: n/a
Mick White wrote:
Frances wrote:

<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>
Sep 19 '05 #7

P: n/a
Lee
Frances said:

Mick White wrote:
Frances wrote:

<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.

Sep 19 '05 #8

P: n/a
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> wrote:
Removing the "var" keyword from the declaration will make
the variable global.


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.

Sep 19 '05 #9

P: n/a
Lee wrote:
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......

Sep 19 '05 #10

P: n/a
Frances said the following on 9/19/2005 1:30 PM:
Lee wrote:
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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 19 '05 #11

P: n/a
Lee <RE**************@cox.net> wrote:
That would be nice, if there was a way to do so in Javascript.


<html><script>
var foo;
</script></html>

What is the scope of foo?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #12

P: n/a
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> wrote:
That would be nice, if there was a way to do so in Javascript.


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

Sep 19 '05 #13

P: n/a
Lee <RE**************@cox.net> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #14

P: n/a

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

<script type = "text/javascript">
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:

</script>
</head>
<body>
<script>
<script type = "text/javascript">
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.
</script>
</body>

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


Sep 19 '05 #15

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


Don't forget to include the type attribute:

<script type = "text/javascript">
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:

</script>
</head>
<body>
<script>

<script type = "text/javascript">
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.