Web Hosting Talk







View Full Version : JavaScript Help


nog
09-05-2003, 11:40 PM
Hello,

I'm having a problem with javascript, its quite annoying. I'm sure the solution is simple, but I cannot seem to figure it out.

Code example:

<select name="dropdownbox" onChange="window.document.write('This isn't working.')">
<OPTION>Blah1
<OPTION>Blah2
<OPTION>Blah3
</select>

Now when I try this by clicking something in the box, instead of adding the text to what is already there like normal, it writes it in a new blank page, overwriting the old one.

Question: How do I go about making this so it writes and adds on to the same page, not a new one?

Thanks.

jb4mt
09-06-2003, 12:33 AM
First of all, it IS working "like normal". What you want to do instead is to create a sort of place holder in your document, using a <DIV> tag, which is blank to begin with, but whose content you can change when the select's onChange event fires.

Below is some code from that is a combination of what you have provided and an example from Javascript: The Definitive Guide published by OReilly (comphosting: flame away). It tests for me with IE 6 and Mozilla 1.4, it demonstrates the concepts, and can be altered for your needs.

<html>
<head>
<script>
/**
* This debug function displays plain-text debugging messages in a
* special box at the end of a document. It is a useful alternative
* to using alert() to display debugging messages.
**/
function debug(msg) {
// If we haven't already created a box within which to display
// our debugging messages, then do so now. Note that to avoid
// using another global variable, we store the box node as a property
// of this function.
if (!debug.box) {
// Create a new <div> element
debug.box = document.createElement("div");
// Specify what it looks like using CSS style attributes
debug.box.setAttribute("style",
"background-color: white; " +
"font-family: monospace; " +
"border: solid black 3px; " +
"padding: 10px;");

// And append our new <div> element to the end of the document
document.body.appendChild(debug.box);

// Now add a title to our <div>. Note that the innerHTML property is
// used to parse a fragment of HTML and insert it into the document.
// innerHTML is not part of the W3C DOM standard, but it is supported
// by Netscape 6 and Internet Explorer 4 and later. We can avoid
// the use of innerHTML by explicitly creating the <h1> element,
// setting its style attribute, adding a Text node to it, and
// inserting it into the document, but this is a nice shortcut
debug.box.innerHTML =
"<h1 style='text-align:center'>Debugging Output</h1>";
}

// When we get here, debug.box refers to a <div> element into which
// we can insert our debugging message.
// First, create a <p> node to hold the message
var p = document.createElement("p");
// Now create a text node containing the message, and add it to the <p>
p.appendChild(document.createTextNode(msg));
// And append the <p> node to the <div> that holds the debugging output
debug.box.appendChild(p);
}
</script>
</head>
<body>
<form>
<select name="dropdownbox" onChange="debug('debug')">
<OPTION>Blah1
<OPTION>Blah2
<OPTION>Blah3
</select>
</form></body></html>

nog
09-06-2003, 01:15 AM
Ah, it seems this will work perfectly! Thanks alot. And please excuse my naive statement in the original post.