3
  <script>
 $("#btn1").click(function() {
     $("#logpop").hide("slow");
  });

  </script>
<body>

<div id="logpop">
    <div class="logpop_box">
    <div class="form">
    <input class="input_box" name="email" type="text" placeholder="Woosuk"/>                                  <br/><br/>
    <input class="input_box" name="pass" type="password" placeholder="******"/>  <br/>
    <button id="btn1">Login</button>

    </div>
    </div>
</div>

I want to make div disappear when i click the button. but It dose not disappear. Any thought

1
  • Place your script somewhere after the btn1 element, yet still within the <body></body>, and it will work. Some people like to put their scripts just before the closing </body> tag. Commented Feb 16, 2012 at 0:40

3 Answers 3

6

Your JavaScript needs to be in a document ready function:

$(document).ready(function() {
  $("#btn1").click(function() {
     $("#logpop").hide("slow");
  });
});

Currently your click handler is executing before the DOM has been created, thus it can't be attached.

Sign up to request clarification or add additional context in comments.

Comments

3

You should register your click handler inside a document-ready block:

$(function(){
    $("#btn1").click(...);
});

Your script is running before the element exists, and so no click handler is registered.

Comments

2

Further to what the other answers said, you can't select an element that has not yet been parsed, given that the browser executes inline JavaScript in the order in which it appears while it parses the source document - so at the point your code runs no elements have been parsed. There are two ways to get around this:

  1. Put your code in a $(document).ready() handler (or use an old-school onload event handler).
  2. Move your script block to anywhere after the element(s) it tries to reference. Most people find script blocks in the middle of the document messy, so (unless absolutely necessary) I'd suggest putting it at the end, right before the closing </body> tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.