0

I am currently working on Javascript with Jquery which is showing div block when seconds goes = 0.

Here is my code:

<HTML>
<HEAD>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="./test.css" />
<script>
$(function () {
var count = 5,
    countdown = setInterval(function () {
        $("p.countdown").html(count);
        if (count == 0) {
            $("p.countdown").hide();
            $("p#countblock").show();
            clearInterval(countdown);
        }
        count--;
    }, 1000);
});

$('#clickToHide').click(function() {
        $("p#countblock").hide();
    });
</script>
</HEAD>
<body>

<p id="clickToHide"> X </p>

<p class="countdown"></p>

<p id="countblock"> text to appear </p>
</body>
</HTML>

Here is my CSS:

#countblock{
    display:none;
    width:200px;
    height:50px;
    position:absolute;
    background-color:#f1f1f1;
}

Everything works okay, but when I try to click on "X" it's not hiding the countblock. Where is my mistake and how I can fix it?

3
  • try giving $("p #countblock").hide(); Commented Nov 14, 2013 at 13:17
  • Not working. You can check my code here: www.vemvo.com/test/index.html Commented Nov 14, 2013 at 13:19
  • 1
    i have pasted ur code in jsfiddler and checked its working fine. jsfiddle.net/WS25z Commented Nov 14, 2013 at 13:23

5 Answers 5

1

You code is right, but it still not working because the place it is. It is before the body, so you have to use $(document).ready(); or put the code on the footer of your page.

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

Comments

0

bind your click event after dom

  <script>
    $(function () {
        var count = 5,
        countdown = setInterval(function () {
            $("p.countdown").html(count);
            if (count == 0) {
             $("p.countdown").hide();
            $("p#countblock").show();
            clearInterval(countdown);
         }
          count--;
       }, 1000);
  });
  </script>

<body>

  <p id="clickToHide"> X </p>

  <p class="countdown"></p>

 <p id="countblock"> text to appear </p>

 <script>     
   $('#clickToHide').click(function() {
         $("p#countblock").hide();
     });
  </script>

Comments

0

Tried a jsfiddle and it's working fine. Maybe you have a timing problem: js is loaded before all html elements are there. Try using $( document ).ready

$(document).ready(function() {
     // your code here
});

See working example: http://jsfiddle.net/W7PqB/2/

Comments

0

Its working like this , fiddle

$(function () {
var count = 5,
    countdown = setInterval(function () {
        $("p.countdown").html(count);
        if (count == 0) {
            $("p.countdown").hide();
            $("#countblock").show();
            clearInterval(countdown);
        }
        count--;
    }, 1000);
});

$('#clickToHide').click(function() {

        $("#countblock").hide();
    });

Comments

0

Replace the script from head to body:

<body>
<button id="clickToHide"> X </button>
<p class="countdown"></p>
<p id="countblock"> text to appear </p>
<script>
$('#clickToHide').click(function() {
   $("#countblock").hide();
});
</script>
</body>

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.