1

I am trying to remove a div from my page once i receive an XMPHttpRequest response. I have also tried setting the display of the div to none using: document.getElementById('password').style.display = 'none'; but that didnt work either.

HTML:

 <div class="login-container">
      <div class="title">Login Form</div>
      <div class="form-fields" id="form-fields">
           <form name="login_form" method="post" id="login-form" onsubmit="return submitForm()">
                <input type="text" name="username" id="username" placeholder="Username" required></input>
                <input type="password" name="password" id="password" placeholder="Password" required></input>
                <input type="submit" value="Log in" id="submit-button"></input>
                <input id="sessionID" class="hidden"></input>
           </form>
      </div>
      <div id="success-message" class="hidden">Congratulations! You have been logged in.</div>
      <button id="logout" class="hidden"></button>
 </div>

Javascript Function:

 function sendLoginInfo() {
      var loginRequest = new XMLHttpRequest();
      loginRequest.open("POST", "server/login.php", false);
      loginRequest.setRequestHeader('Content-type', "application/json");
      loginRequest.onreadystatechange = function () {
           if(loginRequest.readyState == 4 && loginRequest.status == 200) {
                sessionID = keyRequest.responseText;
                var el = document.getElementById( 'password' );
                el.parentNode.removeChild( el );

           }
      }
      loginRequest.send(JSON.stringify(JSONFormData));
 }

Update

This problem was solved by changing:

<form name="login_form" method="post" id="login-form" onsubmit="return submitForm()">

To:

<form name="login_form" method="post" id="login-form" onsubmit="submitForm(); return false;">
Micah
  • 13
  • 4

2 Answers2

-1

instead of

document.getElementById('password').style.display = 0;

try

document.getElementById('password').style.display = 'none';
patsimm
  • 476
  • 6
  • 19
-1

You can't really remove divs as far as I know but you can set the visibility of a div to "none" (not "0" as you tried it).

style="display:none"

This makes the div invisible and there won't be any white space.

Dawesign
  • 643
  • 1
  • 7
  • 25
  • the css rules are all set in a different file. for some reason when i try to set the style it is not being set on the element – Micah Oct 30 '14 at 22:38