Click to See Complete Forum and Search --> : HTML within javascript


aodhan
07-30-2003, 05:54 AM
I am trying to place some html code within a javascript function and when the user presses a button to output the selected data to another function.

I am placing this html inside a document.write() so as it writes the form to the screen. This forms contains a select box and a button. When I press button I would like the value selected in select bos sent to other function.

document,write("please select and press button <form name = form2><select id = 'Sel2'><option........></option><INPUT TYPE ='button' VALUE='Add' NAME='AddQ' onclick='getAnswer(Sel2.value)';>");

I am not quiet sure if this can be done so any help would be greatly appreciated or any alternatives.

Fang
07-30-2003, 06:51 AM
Just a bit of tidying:
document.write('please select and press button <form name="form2"><select id = "Sel2"><option>first</option><option>second</option></select><input type ="button" value="add" name="AddQ" onclick="getAnswer(Sel2.value)";></form>');

aodhan
07-30-2003, 08:21 AM
Thanks but when I add that it says that getAnswer is undefined!!! Any ideas

Fang
07-30-2003, 08:51 AM
getAnswer is the function that deals with the selection. It's the function you have defined!

Phil Karras
07-30-2003, 10:42 AM
The problem is that as soon as you do a document.write() it blows away all other HTML and JavaScript in your window.

Since all JS & HTML is now gone, except for what you're now writing, your function, "getAnswer()" is gone as well.

It would be better to use the newer DOM and innerHTML with a div so that you can add the new HTML yto the present HTML/JS page instead of replacing it all with your new HTML.

Fang
07-30-2003, 10:56 AM
The problem is that as soon as you do a document.write() it blows away all other HTML and JavaScript in your window.
Not true! If the document.write() is inline there is no problem.
Only when document.write() is called from a function will this rewrite the page.

aodhan maybe you can give us more of your script to get a better insite as to what you are trying to do.

pyro
07-30-2003, 10:59 AM
Fang is correct, as can be demonstrated with code like this:

<script type="text/javascript">
function testFunction() {
alert ("test");
}
</script>
</head>

<body>
<script type="text/javascript">
document.write('<a href="#" onclick="testFunction();">test</a>');
</script>

Phil Karras
07-30-2003, 11:01 AM
Not true! If the document.write() is inline there is no problem

Ahh, true & yet not true, in-line is OK as long as the page is currently being loaded,
then it is part of the page. However, as soon as the page is done loading
& now your click on a button and now tell it to do a document.write, it
replaces the current HTML page.

Try it and see.

pyro
07-30-2003, 11:06 AM
aodhan - You had a few errors (mostly the fact that you didn't give you <option> tags values, so there were no values... :p ) Also, the way you were referencing it is not cross-browser compatable. Try this:

<script type="text/javascript">
function getAnswer(what) {
alert (what);
}
</script>
</head>

<body>
<script type="text/javascript">
document.write('please select and press button',
'<form name="form2">',
'<select name="Sel2">',
'<option value="first">first</option>',
'<option value="second">second</option>',
'</select>',
'<input type ="button" value="add" name="AddQ" onclick="getAnswer(this.form.Sel2.options[this.form.Sel2.selectedIndex].value)";>',
'</form>');
</script>

Phil Karras - Yes, that is correct, though Fang already said that...

Phil Karras
07-30-2003, 11:14 AM
Phil Karras - Yes, that is correct, though Fang already said that...

No he didn't. He said it only does this if it's in a function

It will also do this if it is inside the button onClick= statement as:
onClick="document.write...." which I believe, was the original question? Or do I have that
wrong? Perhaps I misunderstood the question/problem?

I did notice a comma in the document,write() but didn't
bother with it since the original also said it worked & then it gave the funciton
non-existant error.

Since the function no longer existed I was assuming the document.write was either in
another funciton being called first OR it was directly in the onClick= part.

pyro
07-30-2003, 11:18 AM
Ok, perhaps I assumed incorrectly that what he meant was "if the document.write is called after the page has loaded" ...

Phil Karras
07-30-2003, 11:23 AM
Perhaps, but then it is difficult at times even when English is our native language to
understand each other since we all come with pre-conceived ideas. I added more to
my previous comments because I wasn't sure I understood everything either.

Fang
07-30-2003, 12:40 PM
Enough of the pedantic's on document.write, without any additional information from aodhan we can not solve his problem.

pyro
07-30-2003, 12:46 PM
Unless I alread did: http://forums.webdeveloper.com/showthread.php?s=&postid=75929#post75886