Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't jQuery click function work?

Tags:

jquery

  <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

like image 550
draford Avatar asked May 17 '26 04:05

draford


2 Answers

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.

like image 142
Jivings Avatar answered May 19 '26 17:05

Jivings


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.

like image 26
Ned Batchelder Avatar answered May 19 '26 17:05

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!