0

I started to mess around with JS yesterday but I'm like the biggest noob ^^,

I'm trying to make a button, that can show and hide a text input box with one click.

I have used createElement("input") to create the box but can anyone help me how to use removeChild properly? =D Cause I can't get it to hide. I want to use just html and js for this

Thanks!

/gruffmeister @ scotland

gruffmeister
  • 29
  • 1
  • 8

2 Answers2

0

Here's a pure javascript example for you:

var elem = document.createElement("input");
var btn  = document.createElement("button");
var txt  = document.createTextNode("Hide Input");

elem.id = "hideme";

btn.appendChild(txt);

document.body.appendChild(elem);
document.body.appendChild(btn);

btn.addEventListener( 'click', function(){
    var input = document.getElementById("hideme");
    document.body.removeChild(input);
} );
Ragdata
  • 1,156
  • 7
  • 16
0

Why not use a simple jQuery approach:

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>toggle demo</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
    $(function() {
        $('button').click(function() {
          $('p').toggle;
          $("#myTextField").toggle();
        });
    });
    </script>
    </head>
    <body>
    <button>Toggle</button>
    <input id="myTextField" type="text" name="myTextField" value="">
    </body>
    </html>