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

Document.Write()

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2

P: n/a
Eric Mitchell wrote:
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">
<script type="text/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

--
Rob
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.