Colton.Wagner Posted October 13, 2010 Share Posted October 13, 2010 I am trying to make this script so that when I click on a radio button the text is replaced not appended. I have tried the code replaceChild(). That did not work basically what I am asking is every time I run the function showForm() I want it to replace text in the extra Div. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>programming test.</title> <script type="text/javascript"> function showForm(x) { if (x == "1") { var extra = "This means you have children!"; } else if (x == "0") { var extra = "This means you do not have children!"; } var myText = document.createTextNode(extra); document.getElementById("extra").appendChild(myText); } </script> </head> <body> <p> <form action="?submit=true" method="post"> <h3>Personal Information</h3> <strong>Username:</strong> <input type="text" value="" name="username" /> <br /> <strong>Password:</strong> <input type="password" value="" name="password" /> <br /> <strong>First Name:</strong> <input type="text" value="" name="fname" /> <br /> <strong>Last Name:</strong> <input type="text" value="" name="lname" /> <br /> <strong>Email:</strong> <input type="text" value="" name="email" /> <br /> <strong>Do you have children?</strong> <br /> Yes <input type="radio" value="1" onclick="showForm(this.value)" name="children" /> No <input type="radio" value="0" onclick="showForm(this.value)" name="children" /><br /> <div id="extra" style="color: red;"></div> <input type="submit" value="submit" /><br /> </form> </p> </body> </html> Any help would be greatly appreciated. Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/215749-having-trouble-with-appendchild/ Share on other sites More sharing options...
brianlange Posted October 13, 2010 Share Posted October 13, 2010 just set the value to an empty string before running the appenChild. document.getElementById("extra").innerHTML = ""; document.getElementById("extra").appendChild(myText); Quote Link to comment https://forums.phpfreaks.com/topic/215749-having-trouble-with-appendchild/#findComment-1121852 Share on other sites More sharing options...
Colton.Wagner Posted October 13, 2010 Author Share Posted October 13, 2010 Awesome I was just looking for something a lot more complex like some function name instead of append. It's always the easy stuff that gets me. Now I have a second question when I append html it displays the html as text rather than using the element tags I provide. Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/215749-having-trouble-with-appendchild/#findComment-1121872 Share on other sites More sharing options...
Colton.Wagner Posted October 13, 2010 Author Share Posted October 13, 2010 The answer to my question above lies within the appendChild() nodes do not support element tags or attributes. You must use innerHTMl for the entire thing. Thanks for your help Brian. Quote Link to comment https://forums.phpfreaks.com/topic/215749-having-trouble-with-appendchild/#findComment-1121875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.