Document.Write()

E

Eric Mitchell

Hello all,

I am using the document.write() method to create new content on the same
page, however... I need to create a new button using this method (button in
HTML). Complicating the matter worse, I have a javascript function that
needs to be called in the onClick event of the button. Is there any way of
doing this? Here is my code, which gives me an error because the function is
read as an object, and there is no object...

<script language="javascript">
function firstFunction(){
var buttonType="<input type=button value=Proceed name=contOn
onclick=secondFunction();>"
document.write(buttonType);
}

function secondFunction(){
document.write("Test string");
}
</script>

The first function (firstFunction()) is initiated by a button later in the
code that I didn't feel was necessary to provide. However, as you can see in
the third line of code, in the onClick method of the button... JavaScript
reads secondFunction() as an object, not a function. How can I fix this?
Thank you!

-Eric
 
E

Evertjan.

Eric Mitchell wrote on 24 jan 2005 in comp.lang.javascript:
<script language="javascript">
function firstFunction(){
var buttonType="<input type=button value=Proceed name=contOn
onclick=secondFunction();>"
document.write(buttonType);
}

function secondFunction(){
document.write("Test string");
}
</script>

Document.write() after completion of the page gives
an automatic document.open() and
thereby destroys the content of the page
INCLUDING the javascript content.

So your second function doesn't exist anymore.

Use CSS to hide or display the button,
or innerHTML,
or Child DOM functions.
 
R

RobG

Eric said:
Hello all,

I am using the document.write() method to create new content on the same
page, however... I need to create a new button using this method (button in
HTML). Complicating the matter worse, I have a javascript function that
needs to be called in the onClick event of the button. Is there any way of
doing this?
Yes.

Here is my code, which gives me an error because the function is
read as an object, and there is no object...

You must write valid HTML to the page or use DOM methods to create the
HTML. Alternatively, write the stuff the page and hide/show it using
the CSS display attribute.
<script language="javascript">

function firstFunction(){
var buttonType="<input type=button value=Proceed name=contOn
onclick=secondFunction();>"

You must write valid HTML, so quote all attributes and in particular,
quote the onclick value:

var buttonType = '<input type="button" value="Proceed" name="contOn"'
+ ' onclick="secondFunction();">'
document.write(buttonType);
}

This will completely replace the entire contents of the document. Is
that what you want to do? If not, you need to use some other method of
adding the button to your page, preferably using DOM createElement, and
attaching the onclick event.

If that's what you want to do, put the above input inside a form so you
can just add the new control as another child of the form.
function secondFunction(){
document.write("Test string");
} [...]
reads secondFunction() as an object, not a function. How can I fix this?

Some working code below, but I'm not sure it will help very much.

<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function firstFunction(){
var buttonType='<input type="button" value="Proceed" name="contOn" '
+ ' onclick="secondFunction();">'
document.write(buttonType);
}

function secondFunction() {
alert('I am the second function');
}
</script>
<p>Here is some text</p>
<button onclick="firstFunction();">firstFunction</button>
<p>Here is some more text</p>
</body</html
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top