/    Sign up×
Community /Pin to ProfileBookmark

function document.write?

Bear with me, I’m still trying to get a grasp on Javascript.

I’m trying to set up a series of dropdown box selections and output the results on the same page.

<head>
<SCRIPT type=”text/javascript”>
function definefunction()
{
document.write(“” +variable01+ ” text here “);
}
</SCRIPT>
</head>
<body>
<FORM>
<SELECT NAME=”variable01″ SIZE=”1″><OPTION SELECTED>first option <OPTION>second option</SELECT>
<INPUT TYPE=”button” value=”Calculate” onClick=”definefunction()”>
</FORM>
</body>
</html>

I know my form works, this is a simplified version.

When the “Calculate” button is pressed, the error says ” variable01 not defined “. Well, it was defined in the dropdown menu.
I’ve looked several places to see if I could find a similar Javascript, and I can’t seem to find it in HTML goodies, can someone please help?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 03.2004 — When you use document.write() after the page has fully loaded, you wipe out the document and all information. Only use document.write() to construct variable parts of a web page before the loading has finished (e.g.: in-line with other HTML, not as a fuction called by the user clicking something).
Copy linkTweet thisAlerts:
@anothenauthorAug 03.2004 — I found something, but it didn't fix the problem, but it fixed the error,

{

with(document.forms[0])

}

<head>

<SCRIPT type="text/javascript">

function definefunction()

{

with(document.forms[0])

{

document.write("" +variable01+ " text here ");

}

}

</SCRIPT>

</head>

<body>

<FORM>

<SELECT NAME="variable01" SIZE="1"><OPTION SELECTED>first option <OPTION>second option</SELECT>

<INPUT TYPE="button" value="Calculate" onClick="definefunction()">

</FORM>

</body>

</html>

I understand about everything in this code except the part I added: {with(document.forms[0])}. After adding this, it would output the text, but the variables would output "[object]".
Copy linkTweet thisAlerts:
@anothenauthorAug 03.2004 — wow that was quick Gil, thanks.

hmm, Okay, but the only reason I was attemting it was because of the thread:

[URL=http://www.webdeveloper.com/forum/showthread.php?s=&threadid=39194]http://www.webdeveloper.com/forum/showthread.php?s=&threadid=39194[/URL]

I was able to do calculations on the same page. Isn't there a way I can do a simple input output?
Copy linkTweet thisAlerts:
@CharlesAug 03.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

</head>

<body>

<script type="text/javascript">

<!--

document.write ('<p>Current time is <span id="time">', new Date().toTimeString(), '</span>.</p>');

if (document.getElementById) setInterval("document.getElementById('time').firstChild.data = new Date().toTimeString()", 100)

// -->

</script>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@steelersfan88Aug 03.2004 — Before you leave thinking you know everything ... beware that write() is a method of the document object, not a function.
Copy linkTweet thisAlerts:
@emblemAug 03.2004 — try this
[code=php]
<form name="[COLOR=red]form1[/color]" method="post">
<select name="[COLOR=red]dd1[/color]" size="1">
<option value="[COLOR=red]1[/color]">1</option>
<option value="[COLOR=red]2[/color]">2</option>
<option value="[COLOR=red]3[/color]">3</option>
</select>
<input type="button" onclick="document.write(document.[COLOR=red]form1[/color].[COLOR=red]dd1[/color].options[document.[COLOR=red]form1[/color].[COLOR=red]dd1[/color].selectedIndex].value)" value="OK">
</select>
</form>
[/code]

the red parts are where you need to change

first change all the [COLOR=red]form1[/color] to the forms name then change all the [color=red]dd1[/color] to the name of your select then last but not least change the values(value="[color=red]#[/color]") to what you whant to write in the page when it is selected
Copy linkTweet thisAlerts:
@steelersfan88Aug 03.2004 — Don't use that. That has already been discussed, and is not a good solution.
Copy linkTweet thisAlerts:
@anothenauthorAug 03.2004 — too late, already tried it.

where is the discussion thread? I'll check it out.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...