Jump to content

Having trouble with appendChild();


Colton.Wagner

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/215749-having-trouble-with-appendchild/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.