View Full Version : problem with SIMPLE .replaceChild function...

06-16-2004, 07:18 PM
I have a very simple function that attempts to use .replaceChild to replace a piece of text within a <p> element. It works fine with .appendChild, but fails with .replaceChild. The error is:

Microsoft Jscript runtime error:
Line: 9
Char: 5
Error: Argument not optional

I am very new to Javascript, and am just trying to establish proof of concept for this to incorporate into a site that I am working on. I just ordered the JS Def. Guide this morning, but until then, if anyone can tell me in a textbooky sort of way - what my error is, and more importantly, why (what are required args. for this function, etc) - I would appreciate it +++

TIA - scott

My page is as follows - closing HTML tag removed because it was screwing up the forum CODE tags...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script type="text/javascript">
<!-- hide
function changeText(){
var newText = document.createTextNode ("some new text here");
var para = document.getElementById("T1");
return false;
<!-- show -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<p id="T1">this is the text I want to change.</p>
<a href="#" onClick="changeText();">click me</a>


06-17-2004, 03:52 AM
when u trying replace the tag with a text .... no ? if u use appendChild u are adding the text in the tag....

06-17-2004, 07:15 AM
replaceChild expects two parameters: new child node and old child node.
Try this:

function changeText(){
var newText = document.createTextNode("some new text here");
var para = document.getElementById("T1");
para.replaceChild(newText, para.firstChild);
return false;
<a href="#" onClick="return changeText();">click me</a>

06-17-2004, 05:16 PM
I never put anything in all caps. But really thanks. You've made my day.

06-17-2004, 05:19 PM
Hey. I could not find a link to mark this thread as "resolved". Am I missing something? New to this forum - I'm used to PHP builder...